Lecture 7
PHP Syntax
Reading: 5.2 - 5.4
Except where otherwise noted, the contents of this presentation are Copyright 2009 Marty Stepp and Jessica Miller.
5.2: PHP Basic Syntax
-
5.1: Server-Side Basics
-
5.2: PHP Basic Syntax
-
5.3: Embedded PHP
-
5.4: Advanced PHP Syntax
-
6.1: Parameterized Pages
PHP syntax template
HTML content
<?php
PHP code
?>
HTML content
<?php
PHP code
?>
HTML content ...
- any contents of a
.php
file between <?php
and ?>
are executed as PHP code
- all other contents are output as pure HTML
- can switch back and forth between HTML and PHP "modes"
$a = 3;
$b = 4;
$c = sqrt(pow($a, 2) + pow($b, 2));
math constants
M_PI
|
M_E
|
M_LN2
|
- the syntax for method calls, parameters, returns is the same as Java
int
and float
types
$a = 7 / 2;
$b = (int) $a;
$c = round($a);
$d = "123";
$e = (int) $d;
int
for integers and float
for reals
- division between two
int
values can produce a float
String
type
(5.2.6)
$favorite_food = "Ethiopian";
print $favorite_food[2];
$favorite_food = $favorite_food . " cuisine";
print $favorite_food;
- zero-based indexing using bracket notation
-
there is no
char
type; each letter is itself a String
- string concatenation operator is
.
(period), not +
5 + "2 turtle doves" === 7
5 . "2 turtle doves" === "52 turtle doves"
- can be specified with
""
or ''
String
functions
$name = "Stefanie Hatcher";
$length = strlen($name);
$cmp = strcmp($name, "Brian Le");
$index = strpos($name, "e");
$first = substr($name, 9, 5);
$name = strtoupper($name);
if (condition) {
statements;
} elseif (condition) {
statements;
} else {
statements;
}
- NOTE: although
elseif
keyword is much more common, else if
is also supported
while
loop
(same as Java)
while (condition) {
statements;
}
do {
statements;
} while (condition);
bool (Boolean) type
(5.2.8)
$feels_like_summer = FALSE;
$php_is_rad = TRUE;
$student_count = 217;
$nonzero = (bool) $student_count;
- the following values are considered to be
FALSE
(all others are TRUE
):
-
0
and 0.0
-
""
, "0"
, and NULL
(includes unset variables)
- arrays with 0 elements
- can cast to boolean using
(bool)
FALSE
prints as an empty string (no output); TRUE
prints as a 1
TRUE
and FALSE
keywords are case insensitive
NULL
$name = "Victoria";
$name = NULL;
if (isset($name)) {
print "This line isn't going to be reached.\n";
}
- a variable is
NULL
if
- it has not been set to any value (undefined variables)
- it has been assigned the constant
NULL
- it has been deleted using the
unset
function
- can test if a variable is
NULL
using the isset
function
NULL
prints as an empty string (no output)
$name = array();
$name = array(value0, value1, ..., valueN);
$name[index]
$name[index] = value;
$name[] = value;
$a = array();
$a[0] = 23;
$a2 = array("some", "strings", "in", "an", "array");
$a2[] = "Ooh!";
- to append, use bracket notation without specifying an index
- element type is not specified; can mix types
Array functions
function name(s)
|
description
|
count
|
number of elements in the array
|
print_r
|
print array's contents
|
array_pop ,
array_push ,
array_shift ,
array_unshift
|
using array as a stack/queue
|
in_array ,
array_search ,
array_reverse ,
sort ,
rsort ,
shuffle
|
searching and reordering
|
array_fill ,
array_merge ,
array_intersect ,
array_diff ,
array_slice ,
range
|
creating, filling, filtering
|
array_sum ,
array_product ,
array_unique ,
array_filter ,
array_reduce
|
processing elements
|
Array function example
$tas = array("MD", "BH", "KK", "HM", "JP");
for ($i = 0; $i < count($tas); $i++) {
$tas[$i] = strtolower($tas[$i]);
}
$morgan = array_shift($tas);
array_pop($tas);
array_push($tas, "ms");
array_reverse($tas);
sort($tas);
$best = array_slice($tas, 1, 2);
-
the array in PHP replaces many other collections in Java
-
list, stack, queue, set, map, ...
The foreach
loop
(5.4.4)
foreach ($array as $variableName) {
...
}
$stooges = array("Larry", "Moe", "Curly", "Shemp");
for ($i = 0; $i < count($stooges); $i++) {
print "Moe slaps {$stooges[$i]}\n";
}
foreach ($stooges as $stooge) {
print "Moe slaps $stooge\n";
}
- a convenient way to loop over each element of an array without indexes
5.3: Embedded PHP
-
5.1: Server-Side Basics
-
5.2: PHP Basic Syntax
-
5.3: Embedded PHP
-
5.4: Advanced PHP Syntax
-
6.1: Parameterized Pages
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";
}
?>
- printing HTML tags with
print
statements is bad style and error-prone:
- must quote the HTML and escape special characters, e.g.
\"
- best PHP style is to minimize
print
/echo
statements in embedded PHP code
- but without
print
, how do we insert dynamic content into the page?
PHP expression blocks
(5.3.2)
<?= expression ?>
<h2> The answer is <?= 6 * 7 ?> </h2>
The answer is 42
- PHP expression block: a small piece of PHP that evaluates and embeds an expression's value into HTML
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.
- expression blocks can even go inside HTML tags and attributes