Web Programming Step by Step

Lecture 8
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!

Functions; More File I/O

Functions (5.4.1)

function name(parameterName, ..., parameterName) {
	statements;
}
function bmi($weight, $height) {
	$result = 703 * $weight / $height / $height;
	return $result;
}

Calling functions

name(expression, ..., expression);
$w = 163;  # pounds
$h = 70;   # inches
$my_bmi = bmi($w, $h);

Variable scope: global and local vars

$school = "UW";                   # global
...

function downgrade() {
	global $school;
	$suffix = "Tacoma";             # local

	$school = "$school $suffix";
	print "$school\n";
}

Default parameter values

function name(parameterName = value, ..., parameterName = value) {
	statements;
}
function print_separated($str, $separator = ", ") {
	if (strlen($str) > 0) {
		print $str[0];
		for ($i = 1; $i < strlen($str); $i++) {
			print $separator . $str[$i];
		}
	}
}
print_separated("hello");        # h, e, l, l, o
print_separated("hello", "-");   # h-e-l-l-o

Reading/writing an entire file

# reverse a file
$text = file_get_contents("poem.txt");
$text = strrev($text);
file_put_contents("poem.txt", $text);

Appending to a file

# add a line to a file
$new_text = "P.S. ILY, GTG TTYL!~";
file_put_contents("poem.txt", $new_text, FILE_APPEND);
old contents new contents
Roses are red,
Violets are blue.
All my base,
Are belong to you.
Roses are red,
Violets are blue.
All my base,
Are belong to you.
P.S. ILY, GTG TTYL!~

Splitting/joining strings

$array = explode(delimiter, string);
$string = implode(delimiter, array);
$s  = "CSE 190 M";
$a  = explode(" ", $s);     # ("CSE", "190", "M")
$s2 = implode("...", $a);   # "CSE...190...M"

Example with explode

contents of input file names.txt
Martin D Stepp
Jessica K Miller
Victoria R Kirst
foreach (file("names.txt") as $name) {
	list($first, $mid, $last) = explode(" ", $name);
	?>
	<p> author: <?= $last ?>, <?= $first ?> </p>
	<?php
}

author: Stepp, Marty

author: Miller, Jessica

author: Kirst, Victoria

The htmlspecialchars function

htmlspecialchars returns an HTML-escaped version of a string
$text = "<p>hi 2 u & me</p>";
$text = htmlspecialchars($text);   # "&lt;p&gt;hi 2 u &amp; me&lt;/p&gt;"

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.