Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
$base = $_GET["base"]; $exp = $_GET["exponent"]; $result = pow($base, $exp); print "$base ^ $exp = $result";
http://example.com/exponent.php?base=3&exponent=4
<?php foreach ($_GET as $param => $value) { ?> <p>Parameter <?= $param ?> has value <?= $value ?></p> <?php } ?>
http://example.com/print_params.php?name=Marty+Stepp&sid=1234567
Parameter name has value Marty Stepp
Parameter sid has value 1234567
print_r
or var_dump
on $_GET
for debugging
<label><input type="radio" name="cc" /> Visa</label> <label><input type="radio" name="cc" /> MasterCard</label> <br /> Favorite Star Trek captain: <select name="startrek"> <option>James T. Kirk</option> <option>Jean-Luc Picard</option> </select> <br />
[cc] => on, [startrek] => Jean-Luc Picard
value
attribute<label><input type="radio" name="cc" value="visa" /> Visa</label> <label><input type="radio" name="cc" value="mastercard" /> MasterCard</label> <br /> Favorite Star Trek captain: <select name="startrek"> <option value="kirk">James T. Kirk</option> <option value="picard">Jean-Luc Picard</option> </select> <br />
value
attribute sets what will be submitted if a control is selected[cc] => visa, [startrek] => picard
" "
, "/"
, "="
, "&"
"Marty's cool!?"
→ "Marty%27s+cool%3F%21"
$_GET
and $_POST
arrays automatically decode themGET
vs. POST
requests
GET
: asks a server for a page or data
POST
: submits data to a web server and retrieves the server's response
POST
is more appropriate than GET
GET
requests embed their parameters in their URLs<form action="http://foo.com/app.php" method="post"> <div> Name: <input type="text" name="name" /> <br /> Food: <input type="text" name="meal" /> <br /> <label>Meat? <input type="checkbox" name="meat" /></label> <br /> <input type="submit" /> <div> </form>
if ($_SERVER["REQUEST_METHOD"] == "GET") { # process a GET request ... } elseif ($_SERVER["REQUEST_METHOD"] == "POST") { # process a POST request ... }
$_SERVER
array's "REQUEST_METHOD"
elementArray | Description |
---|---|
$_GET ,
$_POST
|
parameters passed to GET and POST requests |
$_FILES
|
files uploaded with the web request |
$_COOKIE ,
$_SESSION
|
"cookies" used to identify the user (seen later) |
$_SERVER ,
$_ENV
|
information about the web server |
$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";
<form action="http://webster.cs.washington.edu/params.php" method="post" enctype="multipart/form-data"> Upload an image as your avatar: <input type="file" name="avatar" /> <input type="submit" /> </form>
input
tag with type
of file
enctype
attribute of the formpost
(an entire file can't be put into a URL!)enctype
(data encoding type) must be set to multipart/form-data
or else the file will not arrive at the server$_FILES
, not $_POST
$_FILES
is itself an associative array, containing:
name
: the local filename that the user uploadedtype
: the MIME type of data that was uploaded, such as image/jpeg
size
: file's size in bytestmp_name
: a filename where PHP has temporarily saved the uploaded file
<input type="file" name="avatar" />
borat.jpg
as a parameter named avatar
,
$_FILES["avatar"]["name"]
will be "borat.jpg"
$_FILES["avatar"]["type"]
will be "image/jpeg"
$_FILES["avatar"]["tmp_name"]
will be something like "/var/tmp/phpZtR4TI"
$username = $_POST["username"]; if (is_uploaded_file($_FILES["avatar"]["tmp_name"])) { move_uploaded_file($_FILES["avatar"]["tmp_name"], "$username/avatar.jpg"); print "Saved uploaded file as $username/avatar.jpg\n"; } else { print "Error: required file not uploaded"; }
is_uploaded_file(filename)
TRUE
if the given filename was uploaded by the user
move_uploaded_file(from, to)
is_uploaded_file
, then do move_uploaded_file
include
include("filename");
include("header.html"); include("shared-code.php");
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 = "(Wisconsin)"; # 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
$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 )
if (isset($blackbook["marty"])) { print "Marty's phone number is {$blackbook['marty']}\n"; } else { print "No phone number found for Marty Stepp.\n"; }
name(s) | category |
---|---|
isset , array_key_exists
|
whether the array contains value for given key |
array_keys , array_values
|
an array containing all keys or all values in the assoc.array |
asort , arsort
|
sorts by value, in normal or reverse order |
ksort , krsort
|
sorts by key, in normal or reverse order |
foreach
loop and associative arraysforeach ($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