Support for generators has been added via the yield keyword
range(0, 1000000) vs. user supplied function
· Generators provide an easy way to implement simple iterators without the overhead or complexity of implementing a class that implements the Iterator interface middot; Allows you to write code that uses foreach to iterate over a set of data without needing to build an array in memory · Useful in avoiding memory limitations, i.e.function getLines($filepath) { $f = fopen($filepath, 'r'); try { while ($line = fgets($f)) { yield $line; } } finally { fclose($f); } } foreach (getLines("file.txt") as $n => $line) { echo $line; }
Execute code ALWAYS.
try { throw new Exception('hello'); } catch (Exception $e) { echo $e->getMessage(); } finally { // this code will always be run echo ', world'; }· Code that should be run regardless of whether an exception has been thrown or not. · Useful for things like closing file descriptors or resources. · Also useful for cleanup code in a method/function. · You can nest try/catch/finally and throw a new exception · Check the last slide!
A super easy library that uses underlying crypt library.
function register($username, $password) { $hash = password_hash($password, PASSWORD_BCRYPT); // save hash to database } function login($username, $password) { $hash = getHashFromDbByUsername($username); if (password_verify($password, $hash)) { // perform login (store session var) return true; } return false; }
You can optionally supply your own salt and algorithmic cost.
function register($username, $password) { $options = array('salt' => 'someRandomSalt', 'cost' => 12); $hash = password_hash($password, PASSWORD_BCRYPT, $options); // save hash to database }· Default cost is 10. · Cost refers to "work factor". · Work factor affects resulting hash. · Work factor increases the computation time.
function login($username, $password) { $hash = getHashFromDbByUsername($username); if (password_verify($password, $hash)) { // check if hash is in updated format if (password_needs_rehash($hash, PASSWORD_BCRYPT)) { // perform update $hash = password_hash($password, PASSWORD_BCRYPT); // save new hash to database } // perform login (store session var) return true; } return false; }
$array = [ [1, 2], [3, 4], ]; foreach ($array as list($a, $b)) { echo "A: $a; B: $b\n"; }
$people = [ [ 'firstname' => 'John', 'lastname' => 'Doe' ], [ 'firstname' => 'Jane', 'lastname' => 'Doe' ], ]; // contains [ 0 => 'John', 1 => 'Jane' ] $firstnames = array_column($people, 'firstname');
function always_false() { return false; } if (empty(always_false())) { echo "Hello, world."; }
// array dereferencing echo [1, 2, 3][0]; // string dereferencing echo 'PHP'[0];
function foo() { return array(1, 2, 3); } echo foo()[2]; // prints 3 $func = function() { return array('a', 'b', 'c'); }; echo $func()[0]; // prints a
$ php -v PHP 5.4.17RC1 (cli) (built: Jun 22 2013 19:27:26) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies with Zend OPcache v7.0.2, Copyright (c) 1999-2013, by Zend Technologies
APC User Cache is in the works:
APC minus the opcode cache!
· Zend Optimizer+ has a consistent 5-20% rps performance edge over APC
Thanks for pretending to enjoy my banter!