Home » Releases » PHP 5.6 »

PHP 5.6 Code Comparisons

Explore PHP 5.6

Variadic Functions

PHP 5.6 introduces native variadic functions using the ... token. This replaces func_get_args() with clean, type-hintable argument arrays.
Before PHP 5.6
function summarize()
{
    $args = func_get_args();
    // ...
}
PHP 5.6 or Later
function summarize(string ...$items): void
{
    foreach ($items as $item) {
        // ...
    }
}

Argument Unpacking

Argument unpacking allows passing array elements directly as positional function arguments using the ... prefix operator, replacing call_user_func_array().
Before PHP 5.6
$args = [1, 2, 3];
$result = call_user_func_array(
    'add',
    $args
);
PHP 5.6 or Later
$args = [1, 2, 3];
$result = add(...$args);

Constant Scalar Expressions

PHP 5.6 allows scalar expressions involving numeric and string literals, other constants, and operators in contexts where previously only static literal values were allowed.
Before PHP 5.6
define('ONE', 1);
define('TWO', 2);
define('THREE', ONE + TWO);
PHP 5.6 or Later
class Config {
    const TIMEOUT = 30;
    const BUFFER = self::TIMEOUT * 2;
}

Exponentiation Operator

PHP 5.6 introduces the right-associative ** operator for exponentiation, providing a clean syntax alternative to pow().
Before PHP 5.6
$area = pow($radius, 2)
    * 3.14159;
PHP 5.6 or Later
$area = ($radius ** 2)
    * 3.14159;