Princeton University COS 333: Advanced Programming Techniques A Subset of PHP
Princeton University COS 333: Advanced Programming Techniques A Subset of PHP
Princeton University COS 333: Advanced Programming Techniques A Subset of PHP
Program Structure
<?php // Print "hello, world\n" echo "hello, world\n"; ?> ----------------------------------------------------------------------------------<?php function sqr($i) { return $i * $i; } echo sqr(5) . "\n"; ?> ----------------------------------------------------------------------------------Typically embedded in a HTML document.
Including
include "file.php"; include_once "file.php"; require "file.php"; require_once "file.php"; Exec Continues Upon Failure? y y n n File Included Only Once? n y n y
Terminal I/O
Reading from stdin: iValuesRead = fscanf(STDIN, "%d %lf", $i, $d); Writing to stdout: iCharsWritten = printf("%d %f", $i, $d);
Page 1 of 6
Keywords
and, or, xor, __FILE__, exception, __LINE__, array(), as, break, case, class, const, continue, declare, default, die(), do, echo(), else, elseif, empty(), enddeclare, endfor, endforeach, endif, endswitch, endwhile, eval(), exit(), extends, for, foreach, function, global, if, include(), include_once(), isset(), list(), new, print(), require(), require_once(), return(), static, switch, unset(), use, var, while, __FUNCTION__, __CLASS__, __METHOD__, final, php_user_filter, interface, implements, instanceof, public, private, protected, abstract, clone, try, catch, throw, cfunction, this, final, __NAMESPACE__, namespace, goto, __DIR__
Conversions: (int), (integer): (bool), (boolean): (float), (double), (real): (string): (array): (class): (unset):
Operators
non-associative left non-associative right right right non-associative right left left left non-associative non-associative left left left left left left right clone, new [] ++ -~ (int) (float) (string) (array) (object) (bool) @ instanceof ! * / % + - . << >> < <= > >= <> == != === !== & ^ | && || ? : = += -= *= /= .= %= (1) clone and new (2) array() (3) increment/decrement (4) bitwise not, negation (4) typecast (4) typecast (5) type check (6) logical (7) arithmetic (8) arithmetic and string (9) bitwise (10) comparison (11) comparison (12) bitwise and references (13) bitwise (14) bitwise (15) logical (16) logical (17) conditional expression (18) assignment
Page 2 of 6
Statements
Expression statement: expr; Echo statement: echo expr, expr, ...; Compound statement: { statement; statement; ...} Selection statements: if (booleanExpr) statement else statement; Note: false, 0, null, '', "", and array() all indicate logical FALSE; any other value indicates logical TRUE switch (expr) { case (value1): statement; ...; break; case (value2): statement; ...; break; ... default: statement; ...; } Iteration statements: while (booleanExpr) statement; do statement while (expr); for (initexpr; booleanExpr; increxpr) statement; Note: false, 0, null, '', "", and array() all indicate logical FALSE; any other value indicates logical TRUE foreach ($array as $element) statement; foreach ($associativeArray as $key => $value) statement; break; continue; Return statement:
Page 3 of 6
return; return expr; Function call: f(expr, ...); Default is call-by-value. & preceding a formal parameter indicates call-by-reference. Exception handling: try { ... } catch (Exception $e) { ... $e->getMessage() ... } throw new Exception("Description");
Data Structures
Arrays $a = array(1, 2, "three"); $a[] = "four"; // Appends. echo $a[0]; Associative Arrays $aa = array("John" => "rhythm guitar", "Paul" => "bass guitar"); $aa["George"] = "lead guitar"; $aa["Ringo"] = "drums"; echo $aa["Paul"];
Page 4 of 6
Functions on Arrays is_array(), count(), sort(), shuffle(), explode(), extract(), compact(), reset(), end() An array is not an object. Often must use call-by-reference when passing an array to a function. function fill1($a) { $a[] = 5; $a[] = 6; $a[] = 7; } function fill2(&$a) { $a[] = 5; $a[] = 6; $a[] = 7; } $a = array(); fill1($a); // a is unaffected. fill2($a); // a is affected.
Strings
Functions on Strings strlen(), strpos(), strtolower(), strtoupper(), chr(), ord(), explode(), implode(), ltrim(), rtrim(), trim(), substr(), str_replace(), ucwords()
Files
fopen(), fgets(), fscanf(), fprintf(), fclose() as in C $bool = file_exists($file); STDIN, STDOUT, and STDERR are predefined.
Command-Line Arguments
$argc is the argument count $argv is a list $argv[0] is the name of the program ... if ($argc != 3) { echo "Usage: $argv[0] arg1 arg2"; exit(1); }
Superglobal Variables
When run within a web server: $GLOBALS $_SERVER $_GET $_POST $_FILES $_COOKIE $_SESSION $_REQUEST $_ENV All variables Headers, paths, script locations, etc. Data passed to the script via the HTTP GET method Data passed to the script via the HTTP POST method Data uploaded to the script via the HTTP POST method Data passed to the script via HTTP cookies Session data Data from the browser, by default $_GET, $_POST, and $_COOKIE Data passed to the script via environment variables
Page 5 of 6
Etc.
We'll cover other features of PHP throughout the course as necessary.
Page 6 of 6