- Introduced minor features
District 9, by Sony Pictures
Gangnam Style, by PSY
yield
xrange()
class RangeIterator implements Iterator {
private $start;
private $limit;
private $step;
private $current;
public function __construct($start, $limit, $step = 1) {
$this->start = $start;
$this->limit = $limit;
$this->step = $step;
$this->current = $start;
}
public function current() {
return $this->current;
}
public function key() {
return ($this->current - $this->start) / $this->step;
}
public function next() {
$this->current += $this->step;
}
public function rewind() {
$this->current = $this->start;
}
public function valid() {
return ($this->current >= $this->start && $this->current <= $this->limit);
}
}
function xrange($start, $limit, $step = 1) {
return new RangeIterator($start, $limit, $step);
}
function xrange($start, $limit, $step = 1) {
for ($i = $start; $i <= $limit; $i += $step) {
yield $i;
}
}
function xrange($start, $limit, $step = 1) {
for ($i = $start; $i <= $limit; $i += $step) {
yield $i;
}
}
foreach (xrange(1, 9, 2) as $number) {
echo "$number\n";
}
1
3
5
7
9
finally
finally
function do_something() {
$temp = tempnam('/tmp', 'foo');
try {
...
unlink($temp);
} catch (FrameworkException $e) {
...
unlink($temp);
} catch (Exception $e) {
unlink($temp);
throw $e;
}
}
finally
class TemporaryFile {
function __construct($directory, $prefix) {
$this->file = tempnam($directory, $prefix);
}
function __destruct() { unlink($this->file); }
function __toString() { return $this->file; }
}
function do_something() {
$temp = new TemporaryFile('/tmp, 'foo');
try { ... }
catch (FrameworkException $e) { ... }
}
finally
function do_something() {
$temp = tempnam('/tmp', 'FOO');
try {
...
} catch (FrameworkException $e) {
...
} finally {
unlink($temp);
}
}
finally
function f() {
try {
return 'try';
} finally {
return 'finally';
}
}
echo f();
finally
crypt()
echo crypt('foo', '$2a$01234567890123456789a');
$2zJyhpjk3l9E
password_hash($password, $algo, $options)
password_verify($password, $hash)
password_get_info($hash)
password_needs_rehash($hash, $algo, $options)
// Creating a hash:
$hash = password_hash($passwd, PASSWORD_DEFAULT);
// Verifying a password:
if (password_verify($passwd, $hash)) {
// do stuff
}
PASSWORD_DEFAULT
is equivalent to PASSWORD_BCRYPT
right nowpassword_hash()
hashes are forward compatiblePASSWORD_DEFAULT
may change in new versionspassword_compat
zend_extension=${extension_dir}/opcache.so
5.4 | 5.5 | 5.5 + OPcache | |
---|---|---|---|
php.net | 308.73 | 315.77 | 681.76 |
sf 2.1 based | 10.65 | 10.92 | 21.40 |
WordPress 3.5.2 | 23.72 | 24.93 | 79.52 |
foreach
unpacking
$array = [[1, 2], [3, 4]];
foreach ($array as $item) {
list($a, $b) = $item;
// ...
}
foreach
unpacking
$array = [[1, 2], [3, 4]];
foreach ($array as list($a, $b)) {
// ...
}
empty()
empty(my_function());
var_dump([1, 2, 3][0]);
var_dump('PHP'[0]);
var_dump([1, 2, 3][0]);
var_dump('PHP'[0]);
1
P
::class
namespace Name\Space;
class ClassName {}
echo ClassName::class;
::class
namespace Name\Space;
class ClassName {}
echo ClassName::class;
Name\Space\ClassName
DateTime
DateTimeImmutable
add()
, modify()
and sub()
return a new object
DateTime
$dbh->lastInsertId();
$dbh->lastinsertid();
$dbh->lastınsertıd();
function función() {}
FUNCIÓN();
pack()
and unpack()
a
and A
format codes behave like Perl nowZ
for the old behaviourmysql_connect()
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
$db = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('db', $db);
$id = mysql_real_escape_string($_GET['id'], $db);
$st = mysql_query("SELECT name FROM user WHERE id = $id");
while ($row = mysql_fetch_assoc($st)) {
echo "Hi, $row[name]!";
}
$db = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('db', $db);
$id = mysql_real_escape_string($_GET['id'], $db);
$st = mysql_query("SELECT name FROM user WHERE id = $id");
while ($row = mysql_fetch_assoc($st)) {
echo "Hi, $row[name]!";
}
$db = new PDO('mysql:host=localhost;dbname=db', 'user', 'pass');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$st = $db->prepare('SELECT name FROM user WHERE id = :id');
$st->execute(['id' => $_GET['id']]);
while ($row = $st->fetch(PDO::FETCH_ASSOC)) {
echo "Hi, $row[name]!";
}
preg_replace()
/e
modifier
preg_replace(
'(<h([1-6])>(.*?)</h\1>)e',
'"<h$1>" . strtoupper("$2") . "</h$1>"',
$html
);
preg_replace()
/e
modifier
preg_replace_callback(
'(<h([1-6])>(.*?)</h\1>)',
eval('"<h$1>" . strtoupper("$2") . "</h$1>"'),
$html
)
preg_replace()
/e
modifier
preg_replace_callback(
'(<h([1-6])>(.*?)</h\1>)',
eval('"<h$1>" . strtoupper("$2") . "</h$1>"'),
$html
)
preg_replace()
/e
modifier
preg_replace_callback(
'(<h([1-6])>(.*?)</h\1>)',
function (array $matches) {
return "<h$matches[1]>"
.strtoupper($matches[2])
."</h$matches[1]>";
},
$html
)
apt-get build-dep php5
is your friend on Debian-based distros