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.
web service: a software system exposing useful functionality that can be invoked through the internet using common protocols
$name = array(); # create $name = array(value0, value1, ..., valueN); $name[index] # get element value $name[index] = value; # set element value $name[] = value; # append
$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)
count : number of elements in the arrayprint_r : print array's contentsarray_pop,
array_push,
array_shift,
array_unshift
in_array,
array_search,
array_reverse,
sort,
rsort,
shuffle
array_fill,
array_merge,
array_intersect,
array_diff,
array_slice,
range
array_sum,
array_product,
array_unique,
array_filter,
array_reduce
$tas = array("MD", "BH", "KK", "HM", "JP");
for ($i = 0; $i < count($tas); $i++) {
$tas[$i] = strtolower($tas[$i]);
} # ("md", "bh", "kk", "hm", "jp")
$morgan = array_shift($tas); # ("bh", "kk", "hm", "jp")
array_pop($tas); # ("bh", "kk", "hm")
array_push($tas, "ms"); # ("bh", "kk", "hm", "ms")
array_reverse($tas); # ("ms", "hm", "kk", "bh")
sort($tas); # ("bh", "hm", "kk", "ms")
$best = array_slice($tas, 1, 2); # ("hm", "kk")
foreach loopforeach ($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"; # even himself!
}
$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 arrayslistlist($var1, ..., $varN) = array;
$line = "stepp:17:m:94";
list($username, $age, $gender, $iq) = explode(":", $line);
list function accepts a comma-separated list of variable names as parameters
$autobots = array("Optimus", "Bumblebee", "Grimlock");
$autobots[100] = "Hotrod";
count of 4, with 97 blank elements between "Grimlock" and "Hotrod"$blackbook = array(); $blackbook["marty"] = "206-685-2181"; $blackbook["stuart"] = "206-685-9138"; ... print "Marty's number is " . $blackbook["marty"] . ".\n";
"marty" maps to value "206-685-2181"
print "Marty's number is {$blackbook['marty']}.\n";
$name = array(); $name["key"] = value; ... $name["key"] = value;
$name = array(key => value, ..., key => value);
$blackbook = array("marty" => "206-685-2181",
"stuart" => "206-685-9138",
"jenny" => "206-867-5309");
print_r($blackbook);
Array
(
[jenny] => 206-867-5309
[stuart] => 206-685-9138
[marty] => 206-685-2181
)
foreach ($blackbook as $key => $value) {
print "$key's phone number is $value\n";
}
jenny's phone number is 206-867-5309 stuart's phone number is 206-685-9138 marty's phone number is 206-685-2181
# if (!isset($blackbook["marty"]))
if (!array_key_exists("marty", $blackbook)) {
print "No phone number found for Marty Stepp.\n";
}
array_key_exists : whether the array contains a value for the given keyarray_keys, array_values : all keys or all values in the arrayasort, arsort : sorts by value, in normal or reverse orderksort, krsort : sorts by key, in normal or reverse orderhttps://example.com/student_login.php?username=stepp&sid=1234567
username has value stepp, and sid has value 1234567$_REQUEST$user_name = $_REQUEST["username"]; $student_id = (int) $_REQUEST["sid"];
header("Content-type: text/plain");
$base = $_REQUEST["base"];
$exp = $_REQUEST["exponent"];
$result = pow($base, $exp);
print "$base ^ $exp = $result\n";
http://example.com/exponent.php?base=3&exponent=4
3 ^ 4 = 81
header("Content-type: text/plain");
foreach ($_REQUEST as $param => $value) {
print "Parameter $param has value $value\n";
}
http://example.com/print_params.php?name=Marty+Stepp&sid=1234567
Parameter name has value Marty Stepp Parameter sid has value 1234567
if (isset($_REQUEST["creditcard"])) {
$cc = $_REQUEST["creditcard"];
...
} else {
print "You did not submit a credit card number.\n";
...
return;
}
isset function returns TRUE if a given variable/element has a value
array_key_exists function for thisreturn; or the die function
if ($_SERVER["REQUEST_METHOD"] == "GET") {
# process a GET request
...
} elseif ($_SERVER["REQUEST_METHOD"] == "POST") {
# process a POST request
...
}
"REQUEST_METHOD" key of the global $_SERVER array" ", "/", "=", "&""Marty's cool!?" → "Marty%27s+cool%3F%21"file_get_contents,
file_put_contents
file_exists,
filesize,
fileperms,
filemtime,
is_dir,
is_readable,
is_writable,
disk_free_space
copy,
rename,
unlink,
chmod,
chgrp,
chown,
mkdir,
rmdir
scandir,
glob
$text = file_get_contents("schedule.txt");
$lines = explode("\n", $text);
$lines = array_reverse($lines);
$text = implode("\n", $lines);
file_put_contents("schedule.txt", $text);
file_get_contents returns entire contents of a file as a string
file_put_contents writes a string into a file, replacing any prior contents# Returns how many lines in this file are empty or just spaces. function count_blank_lines($file_name) { $text = file_get_contents($file_name); $lines = explode("\n", $text); $count = 0; foreach ($lines as $line) { if (strlen(trim($line)) == 0) { $count++; } } return $count; } ... print count_blank_lines("lecture18-php_web_services.html");
$folder = "images";
$files = scandir($folder);
foreach ($files as $file) {
if ($file != "." && $file != "..") {
print "I found an image: $folder/$file\n";
}
}
scandir returns an array of all files in a given directory".") and parent directory ("..") are included in the array; you probably want to skip them
header("Content-type: text/plain");
header function to specify non-HTML output
header can also be used to send back HTTP error codes:
header("Content-type: text/plain");header("Content-type: application/xml");header("HTTP/1.1 400 Invalid Request");header("HTTP/1.1 404 File Not Found");header("HTTP/1.1 500 Server Error");