Embedded PHP

CSE 190 M (Web Programming), Spring 2008

University of Washington

Except where otherwise noted, the contents of this presentation are © Copyright 2008 Marty Stepp and Jessica Miller and are licensed under the Creative Commons Attribution 2.5 License.

Valid XHTML 1.0 Strict Valid CSS!

Web services vs. embedded PHP

embedded PHP

A bad way to produce HTML in PHP

<?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>My web page</title>\n";
...
?>

Syntax for embedded PHP

html content

<?php
PHP code
?>

html content

Embedded PHP 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>
		<h1>Geneva's Counting Page</h1>
		<p>Watch how high I can count:
<?php
for ($i = 1; $i <= 10; $i++) {
	print "$i\n";
}
?>
		</p>
	</body>
</html>

Embedded PHP as form response

...
	<body>
		<h1>New account created.</h1>
		<p>
<?php
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
...
print "\"Thank you\", $name, for creating an account with $email.\n";
?>
		</p>
	</body>

Practice problem: Photo gallery

Write an embedded PHP page that displays a gallery of photos. The page will show all images in the food_images subfolder of the script's directory on the server as thumbnails (each image uses the galleryimage style class so it will appear 150px tall). Clicking on any image should show that image at full size. (solution, source)

photo gallery

Embedded PHP + print = bad

...	
	<body>
		<h1>Geneva's Counting Page</h1>
		<p>Watch how high I can count:
<?php
for ($i = 1; $i <= 10; $i++) {
	print "$i\n";
}
?>
		</p>
	</body>

PHP expression blocks

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

The answer is 42


Expression block example 1

<?php
$name = $_REQUEST["name"];
$email = $_REQUEST["emailaddress"];
?>
<!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>Account Creation</title></head>
	<body>
		<h1>New account created.</h1>
		<p>
			Thank you, "<?= $name ?>", for creating an
			account with <?= $email ?>.
		</p>
	</body>
</html>

Expression block example 2

<!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 error: unclosed braces

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

Common error fixed

...
	<body>
		<p>Watch how high I can count:
<?php
for ($i = 1; $i <= 10; $i++) {     # PHP mode
?>
			<?= $i ?>                 <!-- HTML mode -->
<?php
}                                  # PHP mode
?>
		</p>
	</body>
</html>

Common error: Missing = sign

...
	<body>
		<p>Watch how high I can count:
<?php
for ($i = 1; $i <= 10; $i++) {
?>
			<? $i ?>
<?php
}
?>
		</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>
	<body>
		<h1>This is a level 1 heading.</h1>
		<h2>This is a level 2 heading.</h2>
		<h3>This is a level 3 heading.</h3>
	</body>

Practice problem: Improved gallery

Update the photo gallery page's code to use PHP expression blocks. The code should not contain any print statements.

If you have time, add the ability for the user to upload a new image to the gallery. Have the gallery submit its POST data to itself. (solution, source)

photo gallery 2