Asked  2 Years ago    Answers:  5   Viewed   89 times

Is it possible to alias a function with a different name in PHP? Suppose we have a function with the name sleep. Is there a way to make an alias called wait?

By now I'm doing like this:

function wait( $seconds ) {



PHP 5.6+ only

Starting with PHP 5.6 it is possible to alias a function by importing it:

use function sleep as wait;

There's also an example in the documentation (see "aliasing a function").

Tuesday, November 15, 2022

Arrow functions in PHP are introduced in PHP 7.4. They are a little different.

The fn keyword

The new fn keyword is now a reserved keyword.

Previously, we used to continue using function keyword.

$add = function ($valone,$valtwo) {
    return $valone + $valtwo;
$add(1,2) // 3

With the advent of new arrow functions:

$add = fn($valone,$valtwo) => $valone + $valtwo;
$add(1,2) // 3

Parent scope

Earlier, we have to follow with the usage of the keyword use for the involvement of a variable from the parent scope.

$y = 1;
$fn = function ($x) use ($y) {
    return $x + $y;
echo $fn(2); // 3

The expression defined in the parent scope will be implicitly captured by-value.

$y = 1;
$fn = fn($x) => $x + $y;
echo $fn(2); // 3

The above follows for $this variable inside class methods.

class foo {
   public function test() {
       $context = fn() => var_dump($this);
$test = new foo();
$test->test();  // object(foo)#1 (0) { }

Just like previously, we used to perform our operations by using the use keyword to take a variable from the parent scope, so this means that we cannot write the value of the variable from the function into the upper scope.

$y = 1;
$fn = fn() => $y++;
$fn(); // Has no effect
echo $y  // 1

If we are thinking of assigning another variable's value from the closure then this also will not work

$y = 1;
$f = 0;
$fn = fn() => $f = $y + 1;
echo $f; // 0

Function signatures

This is completely new in PHP, this allows us the define the type of function, variable and the value the function is returning

fn(int $x) => $x; // the argument type must be (int)
fn(): int => $x; // type of return value (int)

Errors are thrown when the defined argument type is not placed in the argument when calling the function. The error can be caught by using the TypeError type

$var = 10;
$int_fn = fn(int $x): int => $x;
var_dump($int_fn($var)); // int(10)
try {
} catch (TypeError $e) {
    echo $e->getMessage(), "n"; // Argument 1 passed to {closure}() must be of the type int, string given, called in x on line y

By PHP 7.1, they support the ?type in arguments which allows the argument to be null too.

$funn = fn(?int... $args): array => $args;
var_dump($funn(20, null, 30)); // Array(3) { [0]=> int(20) [1]=> NULL [2]=> int(30) }

If you supply a string or anything else rather than int to the above function, then you'll get an error

Argument passed to {closure}() must be of the type int or null, string given, called in x on line y

Nested arrow functions

$var = 6;
var_dump((fn() => fn() => $var)()());  // int(6)
var_dump((fn() => function() use($var) { return $var; })()()); // int(6)

Any possible errors inside the closure are not thrown unless called

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
$b = 1;
fn() => $b + $c; // no error, nothing

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
$b = 1;
(fn() => $b + $c)(); // Notice: Undefined variable: c in the location on line x

If error reporting is off then you'll just get int(1)

How to use PHP. 7.4 now?
For quick online testing just paste these code there

For your native system, I Just cloned this branch of php-src and compiled it using GCC and make. I did my testing via a test.php file and command line to check if everything works.

Core reference -

Thursday, September 29, 2022

You're looking not for sorting your array, but for filtering it - feel the difference.

In PHP, there's array_filter() to do this. If you want to create flexible function, you can act like this:

function filterArray($data, $key, $value)
   return array_filter($data, function($row) use ($key, $value)
      return $row[$key]==$value;

$class = array(
        'e1' => array('nume' => 'Nitu', 'prenume' => 'Andrei', 'sex' => 'm', 'varsta' => 23),
        'e2' => array('nume' => 'Nae', 'prenume' => 'Ionel', 'sex' => 'm', 'varsta' => 27),
        'e3' => array('nume' => 'Noman', 'prenume' => 'Alice', 'sex' => 'f', 'varsta' => 22),
        'e4' => array('nume' => 'Geangos', 'prenume' => 'Bogdan', 'sex' => 'm', 'varsta' => 23),
        'e5' => array('nume' => 'Vasile', 'prenume' => 'Mihai', 'sex' => 'm', 'varsta' => 25)

$result = filterArray($class, 'sex', 'm');
Thursday, November 24, 2022
function round_up($value, $precision=0) {
    $power = pow(10,$precision);
    return ceil($value*$power)/$power;
function round_down($value, $precision=0) {
    $power = pow(10,$precision);
    return floor($value*$power)/$power;
Friday, August 5, 2022

add these code into the top of your script

ini_set('magic_quotes_runtime', 0);
Friday, August 26, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages