Web Programming Step by Step

Lecture 7
Embedded PHP

Reading: 5.3 - 5.5

Except where otherwise noted, the contents of this presentation are Copyright 2010 Marty Stepp and Jessica Miller.

Valid XHTML 1.1 Valid CSS!

5.2: PHP Basic Syntax

PHP syntax template

HTML content

	<?php
		PHP code
	?>

HTML content

	<?php
		PHP code
	?>

HTML content ...

Math operations

$a = 3;
$b = 4;
$c = sqrt(pow($a, 2) + pow($b, 2));
math functions
abs ceil cos floor log log10 max
min pow rand round sin sqrt tan
math constants
M_PI M_E M_LN2

int and float types

$a = 7 / 2;               # float: 3.5
$b = (int) $a;            # int: 3
$c = round($a);           # float: 4.0
$d = "123";               # string: "123"
$e = (int) $d;            # int: 123

String type (5.2.6)

$favorite_food = "Ethiopian";
print $favorite_food[2];            # h

Interpreted strings

$age = 16;
print "You are " . $age . " years old.\n";
print "You are $age years old.\n";    # You are 16 years old.

String functions

# index  0123456789012345
$name = "Stefanie Hatcher";
$length = strlen($name);              # 16
$cmp = strcmp($name, "Brian Le");     # > 0
$index = strpos($name, "e");          # 2
$first = substr($name, 9, 5);         # "Hatch"
$name = strtoupper($name);            # "STEFANIE HATCHER"
NameJava Equivalent
strlen length
strpos indexOf
substr substring
strtolower, strtoupper toLowerCase, toUpperCase
trim trim
explode, implode split, join
strcmp compareTo

bool (Boolean) type (5.2.8)

$feels_like_summer = FALSE;
$php_is_rad = TRUE;

$student_count = 217;
$nonzero = (bool) $student_count;     # TRUE
  • TRUE and FALSE keywords are case insensitive

5.3: Embedded PHP

Printing HTML tags in PHP = bad style

<?php
print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n";
print " \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n";
print "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
print "  <head>\n";
print "    <title>Geneva's web page</title>\n";
...
for ($i = 1; $i <= 10; $i++) {
	print "<p> I can count to $i! </p>\n";
}
?>

PHP expression blocks (5.3.2)

<?= expression ?>
<h2> The answer is <?= 6 * 7 ?> </h2>

The answer is 42

Expression block example

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head><title>CSE 190 M: Embedded PHP</title></head>	
	<body>
		<?php for ($i = 99; $i >= 1; $i--) { ?>
			<p> <?= $i ?> bottles of beer on the wall, <br />
				  <?= $i ?> bottles of beer. <br />
				  Take one down, pass it around, <br />
				  <?= $i - 1 ?> bottles of beer on the wall. </p>
		<?php } ?>
	</body>
</html>

Common errors: unclosed braces, missing = sign

	<body>
		<p>Watch how high I can count:
			<?php for ($i = 1; $i <= 10; $i++) { ?>
				<? $i ?>
		</p>
	</body>
</html>

Complex expression blocks

	<body>
		<?php for ($i = 1; $i <= 3; $i++) { ?>
			<h<?= $i ?>>This is a level <?= $i ?> heading.</h<?= $i ?>>
		<?php } ?>
	</body>

This is a level 1 heading.

This is a level 2 heading.

This is a level 3 heading.