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.
JavaScript already allows us to create dynamic, programmable web pages. Why use a server-side language instead of JavaScript?
There are many other options for server-side languages: Ruby on Rails, JSP, ASP.NET, etc. Why choose PHP?
verb(noun)
rather than noun.verb()
)
The following contents could go into a file hello.php
:
<?php header("Content-type: text/plain"); print "Hello, world!\n"; print "\n"; print "This is my first PHP program.\n"; ?>
<?php
and ends with ?>
.html
file (static content): server just sends that file.php
file (dynamic content): server reads it, runs any script code inside it, then sends result across the network
.php
page on your local hard drive; you'll either see nothing or see the PHP source code.php
file will run the program and send you back its outputprint
print "text";
print "Hello, World!"; print "Escape \"chars\" are the SAME as in Java!\n"; print "You can have line breaks in the string and they'll show up"; print 'A string can use "single-quotes". It\'s cool!';
echo
instead of print
print("Hello, world!");
header("Content-type: text/plain");
header
function to specify non-HTML output
$name = expression;
$user_name = "PinkHeartLuvr78"; $age = 16; $drinking_age = $age + 5; $this_class_rocks = TRUE;
$
, on both declaration and usage
+ - * / % . ++ --
= += -= *= /= %= .=
== != === !== > < >= <=
&& || !
==
just checks value ("5.0" == 5
is TRUE
)===
also checks type ("5" === 5
is FALSE
)5 < "7"
is TRUE
int
and float
types$a = 7 / 2; # float: 3.5 $b = (int) $a; # int: 3 $c = round($a); # float: 4.0 $d = "123"; # string: "123" $e = (int) $d; # int: 123
int
for integers and float
for realsint
values can produce a float
# single-line comment // single-line comment /* multi-line comment */
#
is also allowed
#
comments instead of //
#
and will use it in our examplesString
type
$favorite_food = "Ethiopian";
print $favorite_food[2]; # h
.
(period), not +
5 + "2 turtle doves" === 7
5 . "2 turtle doves" === "52 turtle doves"
""
or ''
String
functions$name = "Kenneth Kuan"; $length = strlen($name); # 12 $cmp = strcmp($name, "Jeff Prouty"); # > 0 $index = strpos($name, "e"); # 1 $first = substr($name, 8, 4); # "Kuan" $name = strtoupper($name); # "KENNETH KUAN"
Name | Java/JS Equivalent |
---|---|
explode , implode |
split , join |
strlen |
length |
strcmp |
compareTo |
strpos |
indexOf |
substr |
substring |
strtolower , strtoupper |
toLowerCase , toUpperCase |
trim |
substring |
$age = 16;print "You are " . $age . " years old.\n";print "You are $age years old.\n"; # You are 16 years old.
" "
are interpreted
' '
are not interpreted:
print 'You are $age years old.\n'; # You are $age years old.\n
{}
:
print "Today is your $ageth birthday.\n";# $ageth not found print "Today is your {$age}th birthday.\n";
for
loop
(same as Java/JS)
for (initialization; condition; update) { statements; }
for ($i = 0; $i < 10; $i++) { print "$i squared is " . $i * $i . ".\n"; }
$feels_like_summer = FALSE;
$php_is_rad = TRUE;
$student_count = 96;
$nonzero = (bool) $student_count; # TRUE
FALSE
(all others are TRUE
):
0
and 0.0
(but NOT 0.00
or 0.000
)
""
, "0"
, and NULL
(includes unset variables)(bool)
FALSE
prints as an empty string (no output); TRUE
prints as a 1
TRUE
and FALSE
keywords are case insensitiveif/else
statementif (condition) { statements; } elseif (condition) { statements; } else { statements; }
elseif
keyword is much more common, else if
is also supportedwhile
loop
(same as Java/JS)
while (condition) { statements; }
do { statements; } while (condition);
NULL
function name(parameterName, ..., parameterName) { statements; }
function quadratic($a, $b, $c) { return -$b + sqrt($b * $b - 4 * $a * $c) / (2 * $a); }
name(parameterValue, ..., parameterValue);
$x = -2; $a = 3; $root = quadratic(1, $x, $a - 2);
function name(parameterName, ..., parameterName) { statements; }
function print_separated($str, $separator = ", ") { if (strlen($str) > 0) { print $str[0]; for ($i = 1; $i < strlen($str); $i++) { print $sep . $str[$i]; } } }
print_separated("hello"); # h, e, l, l, o print_separated("hello", "-"); # h-e-l-l-o
$school = "UW"; # global ... function downgrade() { global $school; $suffix = "Tacoma"; # local $school = "$school $suffix"; print "$school\n"; }
global
statement at its start