PHP examples

PHP Quick Scripting Reference - Source Code

*** Using PHP ***

<html>
 <head><title>PHP Test</title></head>
<body>
 <?php echo "Hello World"; ?>
</body>
</html>

<?php
  // single-line comment
  #  single-line comment
  /* multi-line
     comment */
?>

*** Variables ***

$myVar;
$myVar = 10;
echo $myVar; // "10"

$myVar = 1;   // int type
$myVar = 1.5; // float type

// Float type evaluated as string type
echo $myVar; // "1.5"

$myInt = 1234; // decimal number
$myInt = 0b10; // binary number (2 decimal)
$myInt = 0123; // octal number (83 decimal)
$myInt = 0x1A; // hexadecimal number (26 decimal)

$myFloat = 1.234;
$myFloat = 3e2; // 3*10^2 = 300

$myBool = true;
$myNull = null; // variable is set to null

$myInt = $myNull + 0;      // numeric context (0)
$myBool = $myNull == true; // bool context    (false)
echo $myNull;              // string context  ("")
echo $myUndefined;         // variable is set to null

*** Operators ***

// Arithmetic operators
$x = 4 + 2; // 6 // addition
$x = 4 - 2; // 2 // subtraction
$x = 4 * 2; // 8 // multiplication
$x = 4 / 2; // 2 // division
$x = 4 % 2; // 0 // modulus (division remainder)

// Combined assignment operators
$x = 0;
$x += 5; // $x = $x+5;
$x -= 5; // $x = $x-5;
$x *= 5; // $x = $x*5;
$x /= 5; // $x = $x/5;
$x %= 5; // $x = $x%5;

// Increment and decrement operators
$x++; // $x += 1;
$x--; // $x -= 1;

$x++; // post-increment
$x--; // post-decrement
++$x; // pre-increment
--$x; // pre-decrement

$x = 5; $y = $x++; // $x=6, $y=5
$x = 5; $y = ++$x; // $x=6, $y=6

// Comparison operators
$x = (2 == 3);  // false // equal to
$x = (2 != 3);  // true  // not equal to
$x = (2 <> 3);  // true  // not equal to (alternative)
$x = (2 === 3); // false // identical
$x = (2 !== 3); // true  // not identical
$x = (2 > 3);   // false // greater than
$x = (2 < 3);   // true  // less than
$x = (2 >= 3);  // false // greater than or equal to
$x = (2 <= 3);  // true  // less than or equal to

$x = (1 ==  "1"); // true  (same value)
$x = (1 === "1"); // false (different types)

// Logical operators
$x = (true && false); // false // logical and
$x = (true || false); // true  // logical or
$x = !(true);         // false // logical not

// Bitwise operators
$x = 5 & 4;  // 101 & 100 = 100 (4) // and
$x = 5 | 4;  // 101 | 100 = 101 (5) // or
$x = 5 ^ 4;  // 101 ^ 100 = 001 (1) // xor (exclusive or)
$x = 4 << 1; // 100 << 1  =1000 (8) // left shift
$x = 4 >> 1; // 100 >> 1  =  10 (2) // right shift
$x = ~4;     // ~00000100 = 11111011 (-5) // invert

$x=5; $x &= 4;  // 101 & 100 = 100 (4) // and
$x=5; $x |= 4;  // 101 | 100 = 101 (5) // or
$x=5; $x ^= 4;  // 101 ^ 100 = 001 (1) // xor
$x=5; $x <<= 1; // 101 << 1  =1010 (10)// left shift
$x=5; $x >>= 1; // 101 >> 1  =  10 (2) // right shift

// Additional logical operators
// Same as: $a = (true && false);
$a = true && false; // $a is false

// Same as: ($a = true) and false;
$a = true and false; // $a is true

$a = (true xor true); // false

*** String ***

$a = 'Hello';

$b = $a . ' World'; // Hello World
$a .= ' World';     // Hello World

$c = 'World';
echo "Hello $c"; // "Hello World"
echo 'Hello $c'; // "Hello $c"

$s = <<<LABEL
Heredoc (with parsing)
LABEL;

$s = <<<'LABEL'
Nowdoc (without parsing)
LABEL;

$s = "HellonWorld";
echo "Hello<br>World";
$s = 'It's'; // "It's"

$s = 'Hello';
$s[0] = 'J';
echo $s; // "Jello"

$s[strlen($s)-1] = 'y';
echo $s; // "Jelly"

$a = 'test';
$b = 'test';
$c = ($a == $b); // true

*** Arrays ***

// Numeric arrays
$a = array(1,2,3);
$a = [1,2,3];

$a[0] = 1;
$a[1] = 2;
$a[2] = 3;

$a[3] = 4;
$a[] = 5; // $a[4]

echo "$a[0] $a[1] $a[2] $a[3]"; // "1 2 3 4"

// Associative arrays
$b = array('one' => 'a', 'two' => 'b', 'three' => 'c');

$b['one']   = 'a';
$b['two']   = 'b';
$b['three'] = 'c';

echo $b['one'] . $b['two'] . $b['three']; // "abc"

$c = array(0 => 0, 1 => 1, 2 => 2);
$e = array(5 => 5, 6);

// Mixed arrays
$d = array(0 => 1, 'foo' => 'bar');
echo $d[0] . $d['foo']; // "1bar"

// Multi-dimensional arrays
$a = array(array('00', '01'), array('10', '11'));

$a[0][0] = '00';
$a[0][1] = '01';
$a[1][0] = '10';
$a[1][1] = '11';

echo $a[0][0] . $a[0][1] . $a[1][0] . $a[1][1];

$b = array('one' => array('00', '01'));
echo $b['one'][0] . $b['one'][1]; // "0001"

$c[][][][] = "0000"; // four dimensions

*** Conditionals ***

// If statement
if ($x == 1) {
  echo "x is 1";
}
elseif ($x == 2) {
  echo "x is 2";
}
else {
  echo "x is something else";
}

if ($x == 1)
  echo "x is 1";
elseif ($x == 2)
  echo "x is 2";
else
  echo "x is something else";

switch ($x)
{
  case 1: echo "x is 1"; break;
  case 2: echo "x is 2";  break;
  default: echo "x is something else";
}

if ($x == 1):     echo "x is 1";
elseif ($x == 2): echo "x is 2";
else:             echo "x is something else";
endif;

switch ($x):
  case 1:  echo "x is 1"; break;
  case 2:  echo "x is 2";  break;
  default: echo "x is something else";
endswitch;

<?php if ($x == 1) { ?>
  This will show if $x is 1.
<?php } else { ?>
  Otherwise this will show.
<?php } ?>

<?php if ($x == 1): ?>
  This will show if $x is 1.
<?php else: ?>
  Otherwise this will show.
<?php endif; ?>

// Ternary operator expression
$y = ($x == 1) ? 1 : 2;

// Ternary operator statement
($x == 1) ? $y = 1 : $y = 2;

*** Loops ***

// While loop
$i = 0;
while ($i < 10) { echo $i++; } // 0-9

// Do-while loop
$i = 0;
do { echo $i++; } while ($i < 10); // 0-9

// For loop
for ($i = 0; $i < 10; $i++) { echo $i; } // 0-9
for (;$i < 10;) { echo $i++; }

$a = array(1,2,3);

for($i = 0; $i < sizeof($a); $i++) {
  echo $i; // "123"
}

// Foreach loop
foreach ($a as $v) {
  echo $v; // "123"
}

$a = array ('one' => 1, 'two' => 2, 'three' => 3);

foreach ($a as $k => $v) {
  echo "$k => $v <br>";
}

// Alternative syntax
while ($i < 10): echo $i++; endwhile;

for ($i = 0; $i < 10; $i++): echo $i; endfor;

foreach ($a as $v): echo $v; endforeach;

// Break
for (;;) { break; } // end for

$i = 0;
while ($i++ < 10)
{
  for (;;) { break 2; } // end for and while
}

// Continue
while ($i++ < 10) { continue; } // start next iteration
$i = 0;
while ($i++ < 10)
{
  for (;;) { continue 2; } // start next while iteration
}

$i = 0;
while ($i++ < 10)
{
  switch ($i)
  {
    case 1: continue 2; // start next while iteration
  }
}

// Goto
goto myLabel; // jump to label
myLabel:      // label declaration

loop:
while ($finished === false)
{
  // ...
  if ($try_again) goto loop; // restart loop
}

*** Functions ***

function myFunc()
{
  echo "Hello World";
}

myFunc(); // "Hello World"

// Function parameters
function myFunc($x,$y)
{
  echo $x . $y;
}

myFunc('Hello', ' World'); // "Hello World"

// Default parameters
function myFunc($x, $y = " Earth")
{
  echo $x . $y;
}

myFunc("Hello"); // "Hello Earth"

// Variable parameter list
function myFunc()
{
  $x = func_get_arg(0); // get specified argument
  $y = func_get_arg(1);
  $z = func_get_arg(2);
  echo $x . $y . $z;
}

myFunc('Fee', 'Fi', 'Fo'); // "FeeFiFo"

function myFunc()
{
  $num  = func_num_args();
  $args = func_get_args();
  for ($i = 0; $i < $num; $i++)
    echo $args[$i] . ' ';
}

myFunc('Fee', 'Fi', 'Fo'); // "Fee Fi Fo"

// Return statement
function myFunc()
{
  return 'Hello'; // exit function and return value
}

echo myFunc(); // "Hello"

// Scope
$x = 'Hello'; // global $x

function myFunc()
{
  global $x;      // use global $x in this function
  $x .= ' World'; // change global $x
}

myFunc();
echo $x; // "Hello World"

function myFunc()
{
  $GLOBALS['x'] .= ' World'; // change global $x
}

// Anonymous functions
$say = function($name)
{
  echo "Hello " . $name;
};

$say("World"); // "Hello World"

$x = 1;
$y = 2;

$callback = function($z) use ($x, $y)
{
  return $x + $y + $z;
};

echo $callback(3); // "6"

// Prints a variable number of parameters
function myprint()
{
  $a = func_get_args();
  foreach ($a as $v) { echo $v; }
}

myprint(1,2,3); // "123"

*** Class ***

class MyRectangle
{
  public $x = 5, $y = 10;

  function newArea($a, $b)
  {
    return $a * $b;
  }

  function getArea()
  {
    return $this->newArea($this->x, $this->y);
  }

  function __construct($x, $y)
  {
    $this->x = $x;
    $this->y = $y;
  }

  function __destruct() { echo "Destructed"; }
}

$r = new MyRectangle(5,10);
$r->getArea(10, 2); // 20
$r->x = 5; $r->y = 10;
unset($r); // "Destructed"

// Object comparison
class Flag {
  public $flag = true;
}

$a = new Flag();
$b = new Flag();

$c = ($a == $b);  // true (same values)
$d = ($a === $b); // false (different instances)

*** Inheritance ***

// Parent class (base class)
class Rectangle
{
  public $x, $y;
  function __construct($a, $b)
  {
    $this->x = $a;
    $this->y = $b;
  }
}

// Child class (derived class)
class Square extends Rectangle
{
  function __construct($a)
  {
    parent::__construct($a,$a);
  }
}

$s = new Square(5,10);
$s->x = 5; $s->y = 10;
$s = new Square(5);

final class NotExtendable
{
  final function notOverridable() {}
}

$s = new Square(5);
$s instanceof Square;    // true
$s instanceof Rectangle; // true

*** Access Levels ***

class MyClass
{
  public    $myPublic    = 'public';
  protected $myProtected = 'protected';
  private   $myPrivate   = 'private';

  function test()
  {
    echo $this->myPublic;    // allowed
    echo $this->myProtected; // allowed
    echo $this->myPrivate;   // allowed
  }
}

class MyChild extends MyClass
{
  function test()
  {
    echo $this->myPublic;    // allowed
    echo $this->myProtected; // allowed
    echo $this->myPrivate;   // inaccessible
  }
}

$m = new MyClass();
echo $m->myPublic;    // allowed
echo $m->myProtected; // inaccessible
echo $m->myPrivate;   // inaccessible

class MyVars
{
  var $x, $y; // deprecated property declaration
}

// Object access
class MyClass
{
  private $myPrivate;

  function setPrivate($obj, $val) {
    $obj->myPrivate = $val; // set private property
  }
}

$a = new MyClass();
$b = new MyClass();
$a->setPrivate($b, 10);

*** Static ***

class MyCircle
{
  // Static/class members (only one copy)
  static $pi = 3.14;

  // Instance members (one per object)
  public $r = 10;

  function getArea()
  {
    return self::newArea($this->$r);
  }

  static function newArea($a)
  {
    return self::$pi * $a * $a;
  }
}

echo MyCircle::$pi; // "3.14"
echo MyCircle::newArea(10); // "314"

// Static variables
function add()
{
  static $val = 0;
  echo $val++;
}

add(); // "0"
add(); // "1"
add(); // "2"

// Late static bindings
class MyParent
{
  protected static $val = 'parent';

  public static function getLateBindingVal()
  {
    return static::$val;
  }
}

class MyChild extends MyParent
{
  protected static $val = 'child';
}

echo MyChild::getLateBindingVal(); // "child"

*** Constants ***

class MyCircle
{
  const PI = 3.14;
}

echo MyCircle::PI; // "3.14"

const PI = 3.14;
echo PI; // "3.14"

define('DEBUG', 1);
echo DEBUG; // "1"

define('ONE', 1);     // 1
define('TWO', ONE+1); // 2

define('DEBUG', 1, true);
echo debug; // "1"

if (!defined('PI'))
  define('PI', 3.14);

const PI = 3.14;         // compile-time constant
define('BIT_2', 1 << 2); // run-time constant

// Magic constants
if(!isset($var))
{
  echo '$var not set on line ' . __LINE__;
}

*** Interface ***

interface iMyInterface
{
  public function myMethod();
  const PI = 3.14;
}

interface iComparable
{
  public function compare(iComparable $o);
}

class Circle implements iComparable
{
  public $r;

  public function compare(iComparable $o)
  {
    return $this->r - $o->r;
  }
}

function largest(iComparable $a, iComparable $b)
{
  return ($a->compare($b) > 0) ? $a : $b;
}

*** Abstract ***

 abstract class Shape
{
  private $x = 100, $y = 100;
  abstract public function getArea();
}

class Rectangle extends Shape
{
  public function getArea()
  {
    return $this->x * $this->y;
  }
}

class NonAbstract {}
abstract class MyAbstract extends NonAbstract {}

// Defines default functionality and definitions
abstract class Shape
{
  public $x = 100, $y = 100;
  abstract public function getArea();
}

// Class is a Shape
class Rectangle extends Shape { /*...*/ }

// Defines a specific functionality
interface iComparable
{
  function compare();
}

// Class can be compared
class MyClass implements iComparable { /*...*/ }

*** Traits ***

trait PrintFunctionality
{
  public function myPrint() { echo 'Hello'; }
}

class MyClass
{
  // Insert trait methods
  use PrintFunctionality;
}

$o = new MyClass();
$o->myPrint(); // "Hello"

class MyParent
{
  public function myPrint() { echo 'Base'; }
}

class MyChild extends MyParent
{
  // Overrides inherited method
  use PrintFunctionality;

  // Overrides trait inserted method
  public function myPrint() { echo 'Child'; }
}

$o = new MyChild();
$o->myPrint(); // "Child"

*** Importing Files ***

<?php
include 'myfile.php';
require 'myfile.php'; // halt on error
include_once 'myfile.php'; // include only once
require_once 'myfile.php'; // require only once
?>

 // Auto load
function __autoload($class_name)
{
  include $class_name . '.php';
}

// Attempt to auto include MyClass.php
$obj = new MyClass();

*** Type Hinting ***

function myprint(array $a)
{
  foreach ($a as $v) { echo $v; }
}

myprint( array(1,2,3) ); // "123"

function mycall(callable $callback, $data)
{
  $callback($data);
}

$say = function($myString) { echo $myString; };
mycall( $say, 'Hi' ); // "Hi";

*** Type Conversions ***

$myBool = false;
$myInt = (int)$myBool; // 0

echo $myBool;      // ""
echo (int)$myBool; // "0"

$myInt = 10;
$myArr = (array)$myInt;
$myArr = array($myInt); // same as above
echo $myArr[0]; // "10"

$myObj = (object)$myInt;
echo $myObj->scalar; // "10"

$myNull = (unset)$myInt;
$myNull = null; // same as above

$myVar = 1.2;
settype($myVar, 'int'); // convert variable to int

$myVar = 1.2;
$myVar = (int)$myVar; // 1

$myBool = true;
echo gettype($myBool); // "boolean"

*** Variable Testing ***

// Isset
isset($a); // false

$a = 10;
isset($a); // true

$a = null;
isset($a); // false

// Empty
empty($b); // true

$b = false;
empty($b); // true

// Is_null
$c = null;
is_null($c); // true

$c = 10;
is_null($c); // false
is_null($d); // true (undefined variable notice)

// Unset
$e = 10;
unset($e); // delete $e

$var = 'my value';
$ref = &$var;

unset($ref); // delete only $ref

function myUnset()
{
  // Make $o a reference to $GLOBALS['o']
  global $o;

  // Remove the reference variable
  unset($o);

  // Remove the global variable
  unset($GLOBALS['o']);
}

$var = null; // free memory
unset($var); // delete variable

// Determining types
is_numeric(10.5);   // true  (float)
is_numeric('33');   // true  (numeric string)
is_numeric('text'); // false (non-numeric string)

// Variable information
$a = array('one', 'two', 'three');
print_r($a);

eval('$b = ' . var_export($a, true) . ';');

*** Overloading ***

// Property overloading
class MyProperties
{
  private $data = array();

  public function __set($name, $value)
  {
    $this->data[$name] = $value;
  }

  public function __get($name)
  {
    if (array_key_exists($name, $this->data))
      return $this->data[$name];
  }
}

$obj = new MyProperties();

$obj->a = 1;  // __set called
echo $obj->a; // __get called

// Method overloading
class MyClass
{
  public function __call($name, $args)
  {
    echo "Calling $name $args[0]";
  }

  public static function __callStatic($name, $args)
  {
    echo "Calling $name $args[0]";
  }
}

// "Calling myTest in object context"
(new MyClass())->myTest('in object context');

// "Calling myTest in static context"
MyClass::myTest('in static context');

// Isset and unset overloading
class MyClass
{
  private $data = array();

  public function __set($name, $value) {
    $this->data[$name] = $value;
  }
  public function __get($name) {
    if (array_key_exists($name, $this->data))
      return $this->data[$name];
  }

  public function __isset($name) {
    return isset($this->data[$name]);
  }

  public function __unset($name) {
    unset( $this->data[$name] );
  }
}

$obj = new MyClass();
$obj->name = "Joe";

isset($obj->name); // true
isset($obj->age);  // false

unset($obj->name); // delete property
isset($obj->name); // false

empty($obj->name); // false
empty($obj->age);  // true

*** Magic Methods ***

// Tostring
class MyClass
{
  public function __toString()
  {
    return 'Instance of ' . __CLASS__;
  }
}

$obj = new MyClass();
echo $obj; // "Instance of MyClass"

// Invoke
class MyClass
{
  public function __invoke($arg)
  {
    echo $arg;
  }
}

$obj = new MyClass();
$obj('Test'); // "Test"

// Object serialization
class MyConnection
{
  public $link, $server, $user, $pass;

  public function connect()
  {
    $this->link = mysql_connect($this->server,
                                $this->user,
                                $this->pass);
  }

  public function __sleep()
  {
    return array('server', 'user', 'pass');
  }

  public function __wakeup()
  {
    if(isset($server, $user, $pass))
      $this->connect();
  }
}

$obj = new MyConnection();
// ...

$bin = serialize($obj);   // serialize object
$obj = unserialize($bin); // restore object

// Set state
class Fruit
{
  public $name = 'Lemon';

  static function __set_state(array $myarray)
  {
    $tmp = new Fruit();
    $tmp->name = $myarray['name'];
    return $tmp;
  }
}

$export = var_export(new Fruit(), true);
eval('$MyFruit = ' . $export . ';');

// Object cloning
class Fruit {}

$f1 = new Fruit();
$f2 = $f1;       // copy object reference
$f3 = clone $f1; // copy object

class Apple {}

class FruitBasket
{
  public $apple;

  function __construct() { $apple = new Apple(); }

  function __clone()
  {
    $this->apple = clone $this->apple;
  }
}

*** User Input ***

<html>
<body>
  <form action="MyPage.php" method="post">
    <input type="text" name="myString" />
    <input type="submit" />
  </form>
</body>
</html>

echo $_POST['myString'];
echo $_GET['myString'];
echo $_REQUEST['myString'];

// Security concerns
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
  echo "Invalid email address";

// Sanitize for database use
$name = mysql_real_escape_string($_POST['name']);

// Execute SQL command
$sql = "SELECT * FROM users WHERE user='" . $name . "'";
$result = mysql_query($sql);

// Sanitize for web page use
echo htmlspecialchars($_POST['comment']);

<input type="text" name="myArr[]" />
<input type="text" name="myArr[]" />

<input type="text" name="myArr[name]" />

$val1 = $_POST['myArr'][0];
$val2 = $_POST['myArr'][1];
$name = $_POST['myArr']['name'];

<select name="myArr[]" size="3" multiple="true">
  <option value="apple">Apple</option>
  <option value="orange">Orange</option>
  <option value="pear">Pear</option>
</select>

foreach ($_POST['myArr'] as $item)
  echo $item . ' '; // ex "apple orange pear"

<form action="MyPage.php" method="post"
      enctype="multipart/form-data">
  <input name="myfile" type="file" />
  <input type="submit" value="Upload" />
</form>

// File uploading
$dest = 'upload' . basename($_FILES['myfile']['name']);
$file = $_FILES['myfile']['tmp_name'];
$err  = $_FILES['myfile']['error'];

if($err == 0 && move_uploaded_file($file, $dest))
  echo 'File successfully uploaded';

*** Cookies ***

setcookie("lastvisit", date("H:i:s"), time() + 60*60);

if (isset($_COOKIE['lastvisit']))
  echo "Last visit: " . $_COOKIE['lastvisit'];

// Delete cookie
setcookie("lastvisit", 0, 0);

*** Sessions ***

session_start();

if(isset($_SESSION['views']))
  $_SESSION['views'] += 1;
else
  $_SESSION['views'] = 1;

echo 'Views: ' . $_SESSION['views'];

// Deleting a session
unset($_SESSION['views']); // destroy session variable
session_destroy();         // destroy session

*** Namespaces ***

namespace my;

// Belongs to my namespace
class MyClass {}

<?php
namespace my;
class MyClass {}
?>
<html><body></body></html>

// Nested namespaces
namespace mysub;
class MyClass {} // mysubMyClass

// Alternative syntax
<?php
namespace my
{
  class MyClass {}
?>
<html><body></body></html>
<?php }?>

// Namespaced code
namespace my
{
  const PI = 3.14;
}

// Global code
namespace
{
  echo myPI; // "3.14"
}

// Referencing namespaces
namespace my
{
  class MyClass {}
}

namespace other
{
  // Fully qualified name
  $obj = new myMyClass();
}

namespace my
{
  class MyClass {}
}

namespace
{
  // Qualified name
  $obj = new myMyClass();
}

namespace my
{
  class MyClass {}

  // Unqualified name
  $obj = new MyClass();
}

namespace
{
  function myPrint() { echo 'global'; }
}

namespace my
{
  // Falls back to global namespace
  myPrint(); // "global"
}

namespace my
{
  function myPrint() { echo 'my'; }

  // Call function from global namespace
  myPrint(); // "global"
  myPrint();  // "my"
}

// Namespace aliases
namespace my;
class MyClass {}

namespace foo;
use myMyClass as MyAlias;
$obj = new MyAlias();

namespace foo;
use myMyClass as MyAlias;
{
  $obj = new MyAlias();
}

 namespace foo;

// Same as myMyClass as MyClass
use myMyClass;
$obj = new MyClass();

namespace foo;
use myClass1 as C1, myClass2 as C2;

// Namespace keyword
namespace myname
{
  function myPrint() { echo 'Hi'; }
}
namespace my
{
  namespacenamemyPrint(); // "Hi"
  namemyPrint();           // same as above
}

*** References ***

// Assign by reference
$x = 5;
$r = &$x; // r is a reference to x
$s =& $x; // alternative syntax

$r = 10; // assign value to $r/$x
echo $x; // "10"

// Pass by reference
function myFunc(&$x) { $x .= ' World'; }

$x = 'Hello';
myFunc($x); // reference to x is passed
echo $x;    // "Hello World"

class MyClass { public $x = 1; }

function modifyVal($o)
{
  $o->x = 5;
  $o = new MyClass(); // new local object
}

$o = new MyClass();
modifyVal($o);        // pointer to object is passed
echo $o->x;           // "5"

class MyClass { public $x = 1; }

function modifyRef(&$o)
{
  $o->x = 5;
  $o = new MyClass(); // new object
}

$o = new MyClass();
modifyRef($o);        // reference to object is passed
echo $o->x;           // "1"

// Return by reference
class MyClass
{
  public $val = 10;

  function &getVal()
  {
    return $this->val;
  }
}

$obj = new MyClass();
$myVal = &$obj->getVal();

*** Advanced Variables ***

// Curly syntax
$fruit = 'Apple';
echo "Two {$fruit}s"; // "Two Apples"

for ($i = 1; $i <= 3; $i++)
  ${'x'.$i} = $i;

echo "$x1 $x2 $x3"; // "1 2 3"

for ($i = 'a'; $i <= 'c'; $i++)
  $$i = $i;

echo "$a $b $c"; // "a b c"

// Variable variable names
$a = 'foo';
$$a = 'bar';

echo $foo; // "bar"
echo $$a;  // "bar"

$arr = array('a' => 'Foo', 'b' => 'Bar');

foreach ($arr as $key => $value)
{
  $$key = $value;
}

echo "$a $b"; // "Foo Bar"

// Variable function names
function myPrint($s) { echo $s; }

$func = 'myPrint';
$func('Hello'); // "Hello"

// Variable class names
class MyClass {}

$classname = 'MyClass';
$obj = new $classname();

// Variable class member names

class MyClass
{
  public $myProperty = 10;
}

$obj = new MyClass();
echo $obj->{'myProperty'}; // "10"

*** Error Handling ***

if (is_readable('myfile.txt'))
  $handle = fopen('myfile.txt', 'r');

$handle = @fopen('myfile.txt', 'r');

if ($handle === false)
{
  echo 'File not found.';
}
else
{
  // Read the content of the whole file
  $content = fread($handle, filesize('myfile.txt'));

  // Close the file handler
  fclose($handle);
}

// E_NOTICE – Use of unassigned variable
$a = $x;

// E_WARNING – Missing file
$b = fopen('missing.txt', 'r');

// E_ERROR – Missing function
$c = missing();

// Error handling environment
error_reporting(E_ALL | ~E_NOTICE); // all but E_NOTICE

// During development
error_reporting(E_ALL | E_STRICT);

// During production
ini_set('display_errors','0');

// During development
ini_set('log_errors','1');

// Custom error handlers
set_error_handler('myError', E_ALL | E_STRICT);

function myError($errlvl, $errdesc, $errfile, $errline)
{
  switch($errlvl)
  {
    case E_USER_ERROR:
      error_log("Error: $errdesc", 1, 'me@example.com');
      require_once('my_error_page.php');
      return true;
  }
  return false;
}

// Raising errors
if( !isset($myVar) )
  trigger_error('$myVar not set'); // E_USER_NOTICE

*** Exception Handling ***

function invert($x)
{
  if ($x == 0)
    throw new LogicException('Division by zero');

  return 1 / $x;
}

try
{
  $div = invert(0);
}
catch (LogicException $e)
{
  echo $e->getMessage(); // "Division by zero"
}

// Finally block
$resource = myopen();
try { myuse($resource); }
catch(Exception $e) {}
finally { myfree($resource); }

// Re-throwing exceptions
try { $div = invert(0); }
catch (LogicException $e) { throw $e; }

// Uncaught exception handler
set_exception_handler('myException');

function myException($e)
{
  $file = 'exceptionlog.txt';
  file_put_contents($file,$e->getMessage(),FILE_APPEND);
  require_once('my_error_page.php');
  exit;
}