Princeton University COS 333: Advanced Programming Techniques A Subset of PHP

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

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

include include_once require require_once

Building and Running


php file.php

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

Writing to stderr: iCharsWritten = fprintf(STDERR, "%d %f", $i, $d);

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__

Primitive Data Types


Integer: Floating point number: Interpreted string: Uninterpreted (raw) string: Boolean: Null object: 1, 12345, 01, 012345, 0x1, 0x1DB5 0.0, 1.23, 1.23e4 "hi" 'hi' true, false (case insensitive) null (case insensitive) cast cast cast cast cast cast cast to to to to to to to integer boolean float string array class NULL

Conversions: (int), (integer): (bool), (boolean): (float), (double), (real): (string): (array): (class): (unset):

No declaration statements; variables are created when first assigned a value

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

right left left left left

&= |= ^= <<= >>= => and xor or ,

(18) (19) (20) (21) (22)

assignment logical logical logical many uses

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");

Classes, Objects, and Object References


<?php class MyClass { private $i; public function __construct($i) { $this->i = $i; } public function get() { return $this->i; } public function set($i) { $this->i = $i; } } function main() { $obj1 = new MyClass(5); $obj1->set(10); echo $obj1->get() . "\n"; print_r($obj1); } main(); ?>

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.

Copyright 2011 by Robert M. Dondero, Jr.

Page 6 of 6

You might also like