Skip to content
Muhammet Şafak
tr
Languages 4 min read

PHP 5.6 New Features: Variadic Functions and the ** Operator

An overview of PHP 5.6's variadic argument support and the new exponentiation operator, with practical examples.


PHP 5.6 shipped back in August of the previous year, and I have been using it ever since. It is not a major version bump, but a handful of additions make the daily workflow a little more comfortable. In this post I will focus on two of them in particular: variadic argument support and the exponentiation operator. I will also touch on constant expressions and the new form of use imports for namespaces.

Variable-length arguments: the … operator

Whenever you did not know in advance how many arguments a function would receive, the classic PHP answer was func_get_args():

function topla() {
    $sayilar = func_get_args();
    return array_sum($sayilar);
}

echo topla(1, 2, 3, 4); // 10

This works, but you could not tell what the function accepted just by looking at its signature. There were no parameters, so you had to read the body to understand what was going on.

PHP 5.6 added variadic function support to the language. The ... (splat) operator, when used in a function definition, collects all remaining arguments into an array:

function topla(int ...$sayilar): int {
    return array_sum($sayilar);
}

echo topla(1, 2, 3, 4); // 10

From the function signature alone you can see that this function accepts zero or more integers. It also pairs nicely with type hints (still hints in PHP 5.6).

It can be combined with fixed parameters as well. Fixed parameters always come first:

function birlestir(string $ayrac, string ...$parcalar): string {
    return implode($ayrac, $parcalar);
}

echo birlestir(', ', 'Ali', 'Veli', 'Ayşe'); // Ali, Veli, Ayşe

Beyond that, the ... operator also works on the call side — to “spread” an array into individual arguments:

$degerler = [3, 7, 1, 9, 2];
echo max(...$degerler); // 9

Previously you would have reached for call_user_func_array(). This syntax is far more readable.

func_get_args() vs variadic

func_get_args() still works; existing code is not broken. But what variadic brings is more than syntactic convenience: IDEs and static analysis tools can now understand the parameter. Full-blown static analysis did not exist in the PHP 5.6 era, but even PhpStorm can perform type checking when it sees a ...$sayilar parameter. With func_get_args(), the IDE had nothing to work with.

Exponentiation operator: **

Raising a number to a power in PHP previously required the pow() function:

$sonuc = pow(2, 8); // 256

PHP 5.6 introduced the exponentiation operator **. You can now write:

$sonuc = 2 ** 8; // 256

It reads far more naturally inside mathematical expressions:

$alan   = $yaricap ** 2 * M_PI;
$hacim  = (4 / 3) * M_PI * $yaricap ** 3;

A small piece of syntactic sugar, but if your code is heavy on formulas, the readability improvement is real.

Constant expressions

Prior to PHP 5.6, only literal values could be assigned to constants:

const VERGI = 18;
const VERGI_CARPAN = 1.18; // You could not derive this from VERGI

From PHP 5.6 onwards, simple expressions are allowed inside const and define():

const VERGI      = 18;
const VERGI_ORAN = VERGI / 100;
const CARPAN     = 1 + VERGI / 100;

Instead of manually computing a value and hard-coding it, you can derive it from the source constant. If VERGI changes, CARPAN updates automatically.

Importing functions and constants with use

PHP 5.6 does not yet allow you to import multiple classes from the same namespace in a single use statement (that comes in PHP 7.0), but this release did add support for importing functions and constants:

use function Yardimci\Matematik\topla;
use function Yardimci\Matematik\carp;
use const Yardimci\Matematik\PI;

Previously use was reserved for classes. This addition makes namespaced utility functions much cleaner to work with.

Small but worthwhile additions

None of these changes transform PHP at its core. But small choices like using ... instead of func_get_args(), or writing ** instead of pow(), incrementally improve code readability. There is no special effort required to pick them up — a need arises in a project and the natural syntax just slots in.

PHP 7.0 is expected later this year. It is a much bigger release, bringing type declarations and significant performance improvements. For now I am still on 5.6, and these additions have found their place.

Tags: #PHP
Share:

Comments

Sign in with your GitHub account to join the discussion. Comments are stored in GitHub Discussions.

Related Posts

Search the site

Start typing to search posts, projects and pages.

Esc to close Powered by Pagefind