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.
<?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"; ... ?>
print statements is ugly and error-prone:
\"\n line breaks after each lineprint HTML; it's bad style!html content <?php PHP code ?> html content
.php file that are not between <?php and ?> are output as pure HTML
<!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>
count.php... <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>
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)
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>
print/echo statements as possible in embedded PHP codeprint, how do we insert dynamic content into the page?
<?= expression ?>
<h2>The answer is <?= 6 * 7 ?></h2>
<?= expression ?> is equivalent to:
<?php
print expression;
?>
<?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>
print statement in previous 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>
beer.php
...
<body>
<p>Watch how high I can count:
<?php
for ($i = 1; $i <= 10; $i++) {
?>
<?= $i ?>
</p>
</body>
</html>
{ brace, you must have a matching } brace later (in the same PHP-mode block or a later one)
</body> and </html> above are inside the for loop, which is never closed$end'
...
<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>
= sign
...
<body>
<p>Watch how high I can count:
<?php
for ($i = 1; $i <= 10; $i++) {
?>
<? $i ?>
<?php
}
?>
</p>
</body>
</html>
<? ... ?> is often interpreted as the same as one between <?php ... ?>$i does not produce any output
...
<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>
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)