00:00
ID: 1
ID: 2
class C { public $x = 1; function __construct() { ++$this->x; } function __invoke() { return ++$this->x; } function __toString() { return (string) --$this->x; } } $obj = new C(); echo $obj();
ID: 4
ID: 5
var_dump(boolval(new StdClass()));
ID: 6
ID: 7
$str = '✔ one of the following'; echo str_replace('✔', 'Check', $str);
ID: 8
ID: 9
echo 0x33, ' monkeys sit on ', 011, ' trees.';
ID: 15
class test { public $value = 0; function test() { $this->value = 1; } function __construct() { $this->value = 2; } } $object = new test(); echo $object->value;
ID: 16
var_dump(boolval([]));
ID: 17
$a = array('a', 'b', 'c'); $a = array_keys(array_flip($a));
ID: 18
ID: 21
$array = array("Sue","Mary","John","Anna");
ID: 24
ID: 25
$string = 'Good luck!'; $start = 10; var_dump(substr($string, $start));
ID: 30
ID: 32
for ($i = 0; $i < 1.02; $i += 0.17) { $a[$i] = $i; } echo count($a);
ID: 33
$a = array(0, 1, 2 => array(3, 4)); $a[3] = array(4, 5); echo count($a, 1);
ID: 34
var_dump(boolval(-1));
ID: 37
ID: 38
ID: 39
function increment($val) { ++$val; } $val = 1; increment ($val); echo $val;
ID: 40
echo "22" + "0.2", 23 . 1;
ID: 42
ID: 44
ID: 45
ID: 46
ID: 49
ID: 50
namespace MyFramework\DB; class MyClass { static function myName() { return __METHOD__; } } print MyClass::myName();
ID: 52
ID: 54
ID: 56
ID: 59
ID: 62
$world = 'world'; echo <<<'TEXT'hello $worldTEXT;
ID: 67
$data = '$la2'; $count = strlen($data);
ID: 70
ID: 71
$a = array(true, '0' => false, false => true);
ID: 72
ID: 75
ID: 76
ID: 77
header('Content-Type: text/html; charset=iso-8859-1'); echo '✂✔✝';
ID: 80
md5(rand(), TRUE);
ID: 83
ID: 84
$f = function () { return "hello"; }; echo gettype($f);
ID: 87
function increment (&$val) { return $val + 1; } $a = 1; echo increment ($a); echo increment ($a);
ID: 88
define('PI', 3.14); class T { const PI = PI; } class Math { const PI = T::PI; } echo Math::PI;
ID: 91
$test = '/etc/conf.d/wireless'; substr($test, strrpos($test, '/')); // note that strrpos() is being called, and NOT strpos()
ID: 94
strpos("me myself and I", "m", 2);
ID: 99
ID: 100
<?xml version='1.0'?> <foo> <bar> <baz id="1">One</baz> <baz id="2">Two</baz> </bar> </foo>
ID: 102
abstract class Graphics { abstract function draw($im, $col); } abstract class Point1 extends Graphics { public $x, $y; function __construct($x, $y) { $this->x = $x; $this->y = $y; } function draw($im, $col) { ImageSetPixel($im, $this->x, $this->y, $col); } } class Point2 extends Point1 { } abstract class Point3 extends Point2 { }
ID: 103
<form method="post"> <select name="list"> <option>one</option> <option>two</option> <option>three</option> </select> </form>
ID: 108
function append($str){ $str = $str.'append'; } function prepend(&$str) { $str = 'prepend'.$str; } $string = 'zce'; append(prepend($string)); echo $string;
ID: 110
printf('%010.6f', 22);
ID: 111
function fibonacci (&$x1 = 0, &$x2 = 1) { $result = $x1 + $x2; $x1 = $x2; $x2 = $result; return $result; } for ($i = 0; $i <10; $i++) { echo fibonacci() . ','; }
ID: 112
ID: 114
class Test { public $var = 1; } function addMe(Test $t) { $t->var++; } $t = new Test(); addMe($t); echo $t->var;
ID: 116
ID: 121
function increment ($val) { $_GET['m'] = (int) $_GET['m'] + 1; } $_GET['m'] = 1; echo $_GET['m'];
ID: 122
<form method="post"> <input type="checkbox" name="accept" /> </form>
ID: 125
ID: 126
function ratio ($x1 = 10, $x2) { if (isset ($x2)) { return $x2 / $x1; } } echo ratio (0);
ID: 127
$text = 'This is text'; $text1 = <<<'TEXT' $text TEXT; $text2 = <<<TEXT $text1 TEXT; echo "$text2";
ID: 131
ID: 136
ID: 139
ID: 140
ID: 149
ID: 150
echo '1' . (print '2') + 3;
ID: 151
echo strtr('Apples and bananas', 'ae', 'ea');
ID: 152
echo "1" + 2 * "0x02";
ID: 160
1 ^ 2
ID: 161
ID: 163
$a = array(28, 15, 77, 43);
ID: 178
ID: 180
ID: 185
$pieces = explode("/", "///");
ID: 186
ID: 189
$a = 3; switch ($a) { case 1: echo 'one'; break; case 2: echo 'two'; break; default: echo 'four'; break; case 3: echo 'three'; break; }
ID: 190
id name email ------------------------------------- 1 anna alpha@example.com 2 betty beta@example.org 3 clara gamma@example.net 5 sue sigma@example.info PHP code (assume the PDO connection is correctly established): $dsn = 'mysql:host=localhost;dbname=exam'; $user = 'username'; $pass = '********'; $pdo = new PDO($dsn, $user, $pass); $cmd = "SELECT name, email FROM users LIMIT 1"; $stmt = $pdo->prepare($cmd); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_BOTH); $row = $result[0];
id name email ------------------------------------- 1 anna alpha@example.com 2 betty beta@example.org 3 clara gamma@example.net 5 sue sigma@example.info
ID: 191
ID: 193
ID: 199
<?xml version '1.0'> <document> <bar> <foo>Value</foo> </bar> </document>
ID: 204
//test.php:include "MyString.php"; print ","; print strlen("Hello world!"); //MyString.php:namespace MyFramework\String; function strlen($str) { return \strlen($str)*2; // return double the string length } print strlen("Hello world!");
ID: 205
ID: 206
ID: 208
$a = 'a'; $b = 'b'; echo isset($c) ? $A.$B.$c : ($c = 'c').'d';
ID: 211
$first = "second"; $second = "first"; echo $$$first;
ID: 213
ID: 215
id name email ------------------------------------- 1 anna alpha@example.com 2 betty beta@example.org 3 clara gamma@example.net 5 sue sigma@example.info PHP code (assume the PDO connection is correctly established): $dsn = 'mysql:host=localhost;dbname=exam'; $user = 'username'; $pass = '********'; $pdo = new PDO($dsn, $user, $pass); try { $cmd = "INSERT INTO users (id, name, email) VALUES (:id, :name, :email)"; $stmt = $pdo->prepare($cmd); $stmt->bindValue('id', 1); $stmt->bindValue('name', 'anna'); $stmt->bindValue('email', 'alpha@example.com'); $stmt->execute(); echo "Success!"; } catch (PDOException $e) { echo "Failure!"; throw $e; }
ID: 222
ID: 223
function increment ($val) { $val = $val + 1; } $val = 1; increment($val); echo $val;
ID: 224
class T { const A = 42 + 1; } echo T::A;