: PHP 5.6 vs PHP7
PHP PHP 7 PHP 7 PHP 5.6 PHP PHP 7 Zend Engine 64-bit PHP 7
PHP 7 php7dev vagrant VirtualBox PHP7
Scalar type declarations
PHP7 PHP7 PHP5
Type Hints class array callable self interface PHP7 string boolean integer float
coercive strict
coercive PHP TypeError
<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
int ٣ PHP7
strict declare
strict
<?php
// Strict mode
declare(strict_types=1);
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
: strict PHP PHP
Return type declarations
PHP7
: Scalar type declarations coercive mode strict mode
<?php
// Coercive mode
function sum($a, $b): int {
return $a + $b;
}
// Note that an int will be returned.
var_dump(sum(1, 2.5));
int
strict mode
<?php
//Strict mode
declare(strict_types=1);
function sum($a, $b): int {
return $a + $b;
}
// Note that an Error will be returned.
var_dump(sum(1, 2.5));
Null coalesce operator (??)
ternary isset NULL
isset ternary PHP
<?php
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
$_GET['user']
NULL username NULL username nobody
PHP7 Null coalesce (??)
<?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody'; NULL
<?php
// Coalesces can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody'; PHP7 NULL
$_GET['user'] NULL username
$_POST['user'] NULL username username nobody
Spaceship operator <=>
PHP7 ( expressions) -1 0 1
a b
- -1 a b
0 a b
- 1 a b
<?php
// Integers
echo 1 <=> 1; // 0 equal
echo 1 <=> 2; // -1 less than
echo 2 <=> 1; // 1 greater than
// Floats
echo 1.5 <=> 1.5; // 0 equal
echo 1.5 <=> 2.5; // -1 less than
echo 2.5 <=> 1.5; // 1 greater than
// Strings
echo "a" <=> "a"; // 0 equal
echo "a" <=> "b"; // -1 less than
echo "b" <=> "a"; // 1 greater than
Constant arrays define
constatnt define PHP 5.6 constatnt const PHP7 define
<?php
define('ANIMALS', [
'dog',
'cat',
'bird'
]);
echo ANIMALS[1]; // outputs "cat"
Anonymous classes
new class PHP7
<?php
// new Anonymous class and assigne it to $newClass
$newClass = new class(10) {
private $num;
public function __construct($num)
{
$this->num = $num;
}
};
var_dump($newClass);
new
PHP OOP
Unicode
PHP7 Unicode codepoint Unicode codepoint escape syntax
<?php
echo "\u{aa}";
echo "\u{0000aa}"; //(same as before but with optional leading 0's)
echo "\u{9999}";
Unicode codepoint
Closure::call
Object bindTo
<?php
class A {private $x = 1;}
// Pre PHP 7 code
$getXCB = function() {return $this->x;};
$getX = $getXCB->bindTo(new A, 'A'); // intermediate closure
echo $getX();
A getXCB x porperty property x bindTo
PHP7 call PHP7
<?php
class A {private $x = 1;}
// PHP 7+ code
$getX = function() {return $this->x;};
echo $getX->call(new A);
unserialize
PHP7 unserialize objects unserialize
<?php
//Clasess
class MyClass
{
public $name = 'ali';
};
class MyClass2{};
class MyClass3{};
//instantiate the class and Serilize it into String
$foo = serialize(new MyClass);
//Print the serialized String
echo $foo;
echo "<br>"; //HTML tag
// converts all objects into __PHP_Incomplete_Class object
$data = unserialize($foo, ["allowed_classes" => false]);
//Print the unserialized string
var_dump($data); foo serialize MyClass
O:7:"MyClass":1:{s:4:"name";s:3:"ali";} data unserialize foo MyClass unserialize
unserialize MyClass object(__PHP_Incomplete_Class)
allowed_classes false true
unserialize allowed_classes
<?php
//Clasess
class MyClass
{
public $name = 'ali';
};
class MyClass2{};
class MyClass3{};
//instantiate the class and Serilize it into String
$foo = serialize(new MyClass);
//Print the serialized String
echo $foo;
echo "<br>"; //HTML tag
// converts all objects into __PHP_Incomplete_Class object
$data = unserialize($foo, ["allowed_classes" => false]);
//Print the unserialized string
var_dump($data);
echo "<br>"; //HTML tag
// converts all objects into __PHP_Incomplete_Class object except those of MyClass and MyClass2
$data = unserialize($foo, ["allowed_classes" => ["MyClass", "MyClass2"]]);
//Print the unserialized string
var_dump($data);
echo "<br>"; //HTML tag
// default behaviour (same as omitting the second argument) that accepts all classes
$data = unserialize($foo, ["allowed_classes" => true]);
//Print the unserialized string
var_dump($data); __PHP_Incomplete_Class unserialize
IntlChar
PHP7 Unicode Methods Unicode
<?php
printf('%x', IntlChar::CODEPOINT_MAX);
echo '<br>'; // HTML tag
echo IntlChar::charName('@');
echo '<br>'; // HTML tag
var_dump(IntlChar::ispunct('!'));
echo '<br>'; // HTML tag
: Intl
assert Expectations
assert Expectations Object Throwable
<?php
ini_set('assert.exception', 1);
class CustomError extends AssertionError {}
assert(false, new CustomError('Some error message'));
boolean
true flase
<?php
ini_set('assert.exception', 1);
class CustomError extends AssertionError {}
assert(true == false, new CustomError('True is not false!')); assert
Group use declarations
namespace use PHP7 use
PHP7
<?php
// Pre PHP 7 code
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as C;
use function some\namespace\fn_a;
use function some\namespace\fn_b;
use function some\namespace\fn_c;
use const some\namespace\ConstA;
use const some\namespace\ConstB;
use const some\namespace\ConstC; PHP7
<?php
// PHP 7+ code
use some\namespace\{ClassA, ClassB, ClassC as C};
use function some\namespace\{fn_a, fn_b, fn_c};
use const some\namespace\{ConstA, ConstB, ConstC};
Generator Return Expressions
Generator PHP5.5 PHP return PHP7 return getReturn
<?php
$gen = (function() {
yield 1;
yield 2;
return 3;
})();
foreach ($gen as $val) {
echo $val, PHP_EOL;
} return Generator
getReturn return Generator
<?php
$gen = (function() {
yield 1;
yield 2;
return 3;
})();
foreach ($gen as $val) {
echo $val, PHP_EOL;
}
echo $gen->getReturn(), PHP_EOL; ٣
Generator Generator PHP7
Generator delegation
Generator Generator
<?php
function gen()
{
yield 1;
yield 2;
yield from gen2();
}
function gen2()
{
yield 3;
yield 4;
}
foreach (gen() as $val)
{
echo $val, PHP_EOL;
}
intdiv
indiv
<?php
var_dump(intdiv(10, 3));
Session options
session_start() php.ini read_and_close PHP
<?php
session_start([
'cache_limiter' => 'private',
'read_and_close' => true,
]); session configuration directives
preg_replace_callback_array
regular expression preg_replace_callback() regular expression callback
<?php
$subject = 'Aaaaaa Bbb';
preg_replace_callback_array(
[
'~[a]+~i' => function ($match) {
echo strlen($match[0]), ' matches for "a" found <br>';
},
'~[b]+~i' => function ($match) {
echo strlen($match[0]), ' matches for "b" found';
}
],
$subject
);
regular expression regular expression
CSPRNG Functions
random_bytes() random_int()
list
ArrayAccess PHP7