HTML: Webpage content
CSS: Webpage presentation
JavaScript: Webpage functionality (client-side)
AJAX: Fetching data from the internet
Regular Expressions: Validating input
SQL: Storing data
https://server/path/file
Usually when you type a URL in your browser:
Some URLs actually specify programs that the web server should run, and then send their output back to you as the result:
https://webster.cs.washington.edu/cse154/quote.php
The above URL tells the server webster.cs.washington.edu
to run the
program quote.php
and send back its output
Server-side programs are written using programming languages/frameworks such as:
Web servers contains software to run those programs and send back their output
Each language/framework has its pros and cons
Popularity of languages/frameworks can change quickly:
We will use PHP for server-side programming
Browser requests a .html
file (static content): server
just sends that file
Browser requests a .php
file (dynamic content): server
reads it, runs any script code inside it, then returns the output
print
print "text";
echo "text";
print "Hello, World!\n";
print "Escape \"chars\" are the SAME as in Java!\n";
print "you can have
line breaks in a string.";
print 'A string can use "single-quotes". It\'s cool!';
Hello, world!
Escape "chars" are the SAME as in Java!
You can have line breaks in a
string. A string can use "single-quotes". It's cool!
Some PHP programmers use the equivalent* echo
instead of
print
<?php
//PHP code goes here
echo "This is a test!";
?>
This is a test!
+ - * / %
. ++ --
= += -= *= /= %= .=
Many operators auto-convert types: 5 + "7"
is 12
$name = expression;
$user_name = "Pokemon4Lyfe";
$age = 25;
$age_in_dog_years = $age / 7;
$this_class_rocks = TRUE;
Names are case-sensitive; separate multiple words with _
(as in
$user_name
)
Names always begin with $
on both declaration and usage
Implicitly declared by assignment (type is not written; a "loosely-typed" language)
Basic types:
integer
,
float
,
boolean
,
string
,
array
,
object
,
NULL
Test what type a variable is with is_type
functions, e.g. is_string
gettype
function returns a variable's type as a string (not
often needed)
PHP converts between types automatically in many cases:
string
to int
auto-conversion on +
for
("1" + 1 == 2)
int
to float
auto-conversion on /
for
(3 / 2 == 1.5)
Type-cast with (type):
$age = (int) "21";
$favorite_food = "Ethiopian";
print $favorite_food[2];
0-based indexing using []
bracket notation
String concatenation is .
(period) not +
5 + "2 turtle doves"
produces 75 . "2 turtle doves"
produces "52 turtle doves"Can be specified with "" or ''
# index 0123456789012345
$name = "Kyle Thayer";
$length = strlen($name); # 10
$cmp = strcmp($name, "Whitaker Brand"); # < 0
$index = strpos($name, "y"); # 1
$last = substr($name, 5, 6); # "Thayer"
$name = strtoupper($name); # "KYLE THAYER"
Name | Java Equivalent |
---|---|
strlen | length |
strpos | indexOf |
substr | substring |
strtolower, strtoupper | toLowerCase , toUpperCase |
trim | trim |
explode, implode | split , join |
$age = 16;
print "You are " . $age . " years old.\n";
print "You are $age years old.\n"; # You are 16 years old.
Strings inside " "
are interpreted
Strings inside ' '
are not interpreted:
print 'You are $age years old.\n'; # You are $age years old.
If necessary to avoid ambiguity, you can enclose the variable in {}
:
print "Today is your $ageth birthday.\n"; # ageth not found
print "Today is your {$age}th birthday.\n";
bool
(Boolean) Type
$feels_like_summer = FALSE;
$php_is_rad = TRUE;
$student_count = 217;
$nonzero = (bool) $student_count; # TRUE
The following values are considered to be FALSE
(all others are
TRUE
):
NULL
(includes unset variables)Can cast to boolean using (bool)
FALSE
prints as an empty string (no output); TRUE
prints as
a 1
for (initialization; condition; update) {
statements
}
for ($i = 0; $i < 10; $i++) {
print "$i squared is " . $i * $i . "\n";
}
if (condition) {
statements;
} else if (condition) {
statements;
} else {
statements;
}
Can also use elseif
instead of else if
while (condition) {
statements;
}
do {
statements;
} while (condition);
break
and
continue
keywords also behave as in Java (do not use these in this course)
# single-line comment
// single-line comment
/*
multi-line comment
*/
Like Java, but #
is allowed
#
comments instead of //
#
and will use it in our examples$name = array(); # create
$name = array(value0, value1, ..., valueN);
$name[index] # get element value
$name[index] = value; # set element value
$name[] = value; # append PHP
$a = array(); # empty array (length 0)
$a[0] = 23; # stores 23 at index 0 (length 1)
$a2 = array("some", "strings", "in", "an", "array");
$a2[] = "Ooh!"; # add string to end (at index 5)
function name(parameterName, ..., parameterName) {
statements;
}
function bmi($weight, $height) {
$result = 703 * $weight / $height / $height;
return $result;
}
name(expression, ..., expression);
$w = 163; # pounds
$h = 70; # inches
$my_bmi = bmi($w, $h);