Except where otherwise noted, the contents of this presentation are Copyright 2010 Marty Stepp and Jessica Miller.
function name(parameterName, ..., parameterName) {
statements;
}
function bmi($weight, $height) {
$result = 703 * $weight / $height / $height;
return $result;
}
return statements is implicitly "void"name(expression, ..., expression);
$w = 163; # pounds $h = 70; # inches $my_bmi = bmi($w, $h);
$school = "UW"; # global ... function downgrade() { global $school; $suffix = "Tacoma"; # local $school = "$school $suffix"; print "$school\n"; }
global statement
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
# reverse a file
$text = file_get_contents("poem.txt");
$text = strrev($text);
file_put_contents("poem.txt", $text);
file_get_contents returns entire contents of a file as a string
file_put_contents writes a string into a file, replacing its old contents
# 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!~ |
file_put_contents can be called with an optional third parameter to append (add to the end) rather than overwrite$array = explode(delimiter, string); $string = implode(delimiter, array);
$s = "CSE 190 M";
$a = explode(" ", $s); # ("CSE", "190", "M")
$s2 = implode("...", $a); # "CSE...190...M"
explode and implode convert between strings and arraysexplodenames.txtMartin 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
htmlspecialchars function
htmlspecialchars
|
returns an HTML-escaped version of a string |
$text = "<p>hi 2 u & me</p>";
$text = htmlspecialchars($text); # "<p>hi 2 u & me</p>"
<?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";
}
?>
print statements is bad style and error-prone:
\"print, how do we insert dynamic content into the page?<?= expression ?>
<h2> The answer is <?= 6 * 7 ?> </h2>
<?= expr ?> is equivalent to <?php print expr; ?>
<!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>
<body>
<p>Watch how high I can count:
<?php
for ($i = 1; $i <= 10; $i++) {
?>
<? $i ?>
</p>
</body>
</html>
</body> and </html> above are inside the for loop, which is never closed$end'= in <?=, the expression 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>