Emeka Mbah
Emeka Mbah

Reputation: 17553

How to write multiple expressions in PHP arrow functions

How do you write a PHP arrow function with multiple line expressions?

JavaScript one-liner example:

const dob = (age) => 2021 - age;

PHP one-liner equivalent:

$dob = fn($age) => 2021 - $age;

Javascript multiple line example:

const dob = (age) => {
    if (!age) {
        return null;
    }
    const new_age = 2021 - age; 
    console.log("Your new age is " + new_age);
    return new_age;
}

What is the PHP equivalent for multiple lines within an arrow function?

Upvotes: 36

Views: 16811

Answers (5)

Smruti Ranjan
Smruti Ranjan

Reputation: 302

$getSquare = fn($num) => (function ($temp){
    echo "The square of $temp is : ";
    return $temp**2;
})($num);
echo $getSquare(10);

Upvotes: 0

mickmackusa
mickmackusa

Reputation: 48031

There was some discussion in the PHP Internals mailing list regarding possible syntactic options for introducing the brevity and and data accessibility benefits of multiline arrow functions, but nothing gathered sufficient community support.

Until progress is made on that topic, your multi-line snippet can be written with a conditionally nested Immediately Invoked Functional Expression. My snippet will leverage the fact that when the passed-in age is not null, the returned value is expected to be an integer and it is therefore safe to multiply the calculated value by 1 (the return value of print). Otherwise,

Code: (Demo)

$name = 'mickmackusa';

$dob = fn(?int $age): ?int => !$age
    ? null
    : (
          fn($newAge) => $newAge * print "$name's age is $newAge\n" 
      )(2021 - $age);

var_export($dob(null));
echo "\n---\n";
var_export($dob(31));

Output:

NULL
---
mickmackusa's age is 1990
1990

Alternatively, use another ternary condition instead of multiplying.

Upvotes: 0

Arkemlar
Arkemlar

Reputation: 413

You can use something like (expr1) && (expr2), for example:

$f = fn () => ($hasChanges = true) && ($this->comment = $operation->comment)

Note that expr1 must be true in any case, otherwise expr2 will not be executed.

If expr1 returns nothing (void) or falsy value, then wrap expr1 to (expr1 || true).

Upvotes: -3

Blackhole
Blackhole

Reputation: 20411

Arrow functions in PHP have the form fn (argument_list) => expr. You can only have a single expression in the body of the function.

You can write the expression over multiple lines without problem:

fn($age) =>
      $age
    ? 2021 - $age
    : null

If you really need multiple expressions, then you can simply use anonymous function. The closures aren't automatic as they are with arrow functions, but if you don't need it, it gives exactly the same result.

$dob = function ($age) {
    if (!$age) { return null; }
    $new_age = 2021 - ^$age; 
    echo "Your new age is ". $new_age;

    return $new_age;
}

Upvotes: 34

Nico Haase
Nico Haase

Reputation: 12132

The usage of multiple expressions is not allowed, according to the RFC. It covers the assignment of only a single expression. The extension is discussed further down in the RFC, but not implemented

Upvotes: 10

Related Questions