This article is taken from php.net
1. PHP7.0X
1.1 Scalar Type Declaration
Scalar type declarations have two modes: mandatory (default) and strict mode. The following type parameters can now be used (whether in coercive or strict mode): String (string), Integer (
int
), Float (float), and Boolean (bool
). They extend other types introduced in PHP5: class names, interfaces, arrays and callback types.
<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
The above routine will output
int(9)
1.2 Return value type declaration
PHP 7 added support for return type declarations . Similar to the parameter type declaration , the return type declaration specifies the type of the return value of the function. The available types are the same as those available in the parameter declaration.
<?php
function arraysSum(array ...$arrays): array
{
return array_map(function(array $array): int {
return array_sum($array);
}, $arrays);
}
print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));
The above routine will output
Array
(
[0] => 6
[1] => 15
[2] => 24
)
1.3 The null coalescing operator
Due to the large number of cases where ternary expressions and isset() are used together in everyday use , we added the
??
syntactic sugar of the null coalescing operator ( ). If the variable exists and the value is not **null
**, it returns its own value, otherwise it returns its second operand.
<?php
$username = 'Chon';
echo $username ?? 'nobody';
The above routine will output
Chon
1.4 <=> spaceship operator
<?php
// 整数
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
// 浮点数
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1
// 字符串
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
1.5 Define a constant array by define()
Constants of type Array can now be defined with define() . In PHP 5.6 it can only be
const
defined .
use
Operators have been extended to support importing external functions and constants in a class. The corresponding structures are use function
and use const
.
<?php
define('ANIMALS', [
'dog',
'cat',
'bird'
]);
echo ANIMALS[1];
The above routine will output
cat
1.6 Integer division function intdiv()
The newly added function intdiv() is used to perform integer division operations.
<?php
var_dump(intdiv(10, 3));
The above routine will output
int(3)
2. PHP7.1.X
2.1 Nullable types
The types of parameters and return values can now be made nullable by preceding the type with a question mark. When this feature is enabled, the parameter passed in or the result returned by the function is either the given type or null.
<?php
function testReturn(): ?string
{
return 'elePHPant';
}
var_dump(testReturn());
function testReturn(): ?string
{
return null;
}
var_dump(testReturn());
function test(?string $name)
{
var_dump($name);
}
test('elePHPant');
test(null);
test();
The above routine will output
string(10) "elePHPant"
NULL
string(10) "elePHPant"
NULL
Uncaught Error: Too few arguments to function test(), 0 passed in...
2.2 Class Constant Visibility
<?php
class ConstDemo
{
const PUBLIC_CONST_A = 1;
public const PUBLIC_CONST_B = 2;
protected const PROTECTED_CONST = 3;
private const PRIVATE_CONST = 4;
}
2.3 Symmetric array destructuring
The short array syntax (
[]
) is now an alternative to the list() syntax and can be used to assign the value of an array to some variable (included inforeach
).
<?php
$data = [
[1, 'Tom'],
[2, 'Fred'],
];
// list() style
list($id1, $name1) = $data[0];
// [] style
[$id1, $name1] = $data[0];
// list() style
foreach ($data as list($id, $name)) {
// logic here with $id and $name
}
// [] style
foreach ($data as [$id, $name]) {
// logic here with $id and $name
}
2.4 list() now supports key names
Now list() and its new
[]
syntax support specifying key names inside it. This means it can assign any type of array to some variables (similar to short array syntax)
<?php
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
// list() style
list("id" => $id1, "name" => $name1) = $data[0];
// [] style
["id" => $id1, "name" => $name1] = $data[0];
// list() style
foreach ($data as list("id" => $id, "name" => $name)) {
// logic here with $id and $name
}
// [] style
foreach ($data as ["id" => $id, "name" => $name]) {
// logic here with $id and $name
}
2.5 Supports negative string offsets
All string manipulation functions that support offsets now support negative numbers as offsets, including string subscripts
[]
via OR . In this case, a negative offset is interpreted as an offset from the end of the string.{}
use
Operators have been extended to support importing external functions and constants in a class. The corresponding structures are use function
and use const
.
<?php
var_dump("abcdef"[-2]);
var_dump(strpos("aabbcc", "b", -3));
$string = 'bar';
echo "The last character of '$string' is '$string[-1]'.\n";
The above routine will output
string (1) "e"
int(3)
The last character of 'bar' is 'r'.
3. PHP7.2.x
3.1 Allow overriding of abstract methods (Abstract method)
When an abstract class inherits from another abstract class, the inherited abstract class can override the abstract methods of the inherited abstract class.
<?php
abstract class A
{
abstract function test(string $s);
}
abstract class B extends A
{
// overridden - still maintaining contravariance for parameters and covariance for return
abstract function test($s) : int;
}
The above routine will output
string(10) "elePHPant"
NULL
string(10) "elePHPant"
NULL
Uncaught Error: Too few arguments to function test(), 0 passed in...
Fourth, PHP7.4.x
4.1 Attributes add qualified types
<?php
class User {
public int $id;
public string $name;
}
4.2 Arrow functions provide a more concise way to define functions.
<?php
$factor = 10;
$nums = array_map(fn($n) => $n * $factor, [1, 2, 3, 4]);
// $nums = array(10, 20, 30, 40);
4.3 Null Coalescing Operator Assignment
<?php
$array['key'] ??= computeDefault();
// 等同于以下旧写法
if (!isset($array['key'])) {
$array['key'] = computeDefault();
}
4.4 Array expansion operation
$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
// ['banana', 'orange', 'apple', 'pear', 'watermelon'];