Skip to main content
added 1 character in body
Source Link
Night2
  • 6k
  • 1
  • 14
  • 46

Arrow functions in PHP 7.4

PHP 7.4 is on RC2 version now and hopefully will be releasereleased in about 2 months. List of new features are here (this page can actually be updated when 7.4 is released). In 7.4, finally PHP has got the arrow functions, so not only function answers can be shorter now, but also passing closures to other functions can be a lot shorter too. Here are a few examples:

Return input + 1:

Anonymous function (closure) - 25 bytes - Try it online!

function($n){return$n+1;}

Arrow function - 12 bytes - Try it online!

fn($n)=>$n+1

Multiply items of first input (array of ints) by second input (int):

Anonymous function (closure) - 72 bytes - Try it online!

function($a,$n){return array_map(function($b)use($n){return$b*$n;},$a);}

Arrow function - 38 bytes - Try it online!

fn($a,$n)=>array_map(fn($b)=>$b*$n,$a)

Did you notice that $n is accessible in the inner function without a use $n statement? Yeah that is one of the arrow function features.


As a side note, I could not get arrow functions to work recursively (call the same arrow function inside itself), because we cannot give them a name and storing them as a closure in a variable like $f doesn't make $f accessible withing itself (sad). So this example doesn't work and using $f in first line causes a fatal error:

$f=fn($n)=>$n?$f($n-1):0;
$f(5); // Causes error: "PHP Notice: Undefined variable: f" + "PHP Fatal error: Uncaught Error: Function name must be a string"

But calling an arrow function withing a different arrow function works:

$f1=fn($n)=>$n+1;
$f2=fn($n)=>$f1($n-1);
$f1(2) // Returns 3
$f2(2) // Returns 2

Arrow functions in PHP 7.4

PHP 7.4 is on RC2 version now and hopefully will be release in about 2 months. List of new features are here (this page can actually be updated when 7.4 is released). In 7.4, finally PHP has got the arrow functions, so not only function answers can be shorter now, but also passing closures to other functions can be a lot shorter too. Here are a few examples:

Return input + 1:

Anonymous function (closure) - 25 bytes - Try it online!

function($n){return$n+1;}

Arrow function - 12 bytes - Try it online!

fn($n)=>$n+1

Multiply items of first input (array of ints) by second input (int):

Anonymous function (closure) - 72 bytes - Try it online!

function($a,$n){return array_map(function($b)use($n){return$b*$n;},$a);}

Arrow function - 38 bytes - Try it online!

fn($a,$n)=>array_map(fn($b)=>$b*$n,$a)

Did you notice that $n is accessible in the inner function without a use $n statement? Yeah that is one of the arrow function features.


As a side note, I could not get arrow functions to work recursively (call the same arrow function inside itself), because we cannot give them a name and storing them as a closure in a variable like $f doesn't make $f accessible withing itself (sad). So this example doesn't work and using $f in first line causes a fatal error:

$f=fn($n)=>$n?$f($n-1):0;
$f(5); // Causes error: "PHP Notice: Undefined variable: f" + "PHP Fatal error: Uncaught Error: Function name must be a string"

But calling an arrow function withing a different arrow function works:

$f1=fn($n)=>$n+1;
$f2=fn($n)=>$f1($n-1);
$f1(2) // Returns 3
$f2(2) // Returns 2

Arrow functions in PHP 7.4

PHP 7.4 is on RC2 version now and hopefully will be released in about 2 months. List of new features are here (this page can actually be updated when 7.4 is released). In 7.4, finally PHP has got the arrow functions, so not only function answers can be shorter now, but also passing closures to other functions can be a lot shorter too. Here are a few examples:

Return input + 1:

Anonymous function (closure) - 25 bytes - Try it online!

function($n){return$n+1;}

Arrow function - 12 bytes - Try it online!

fn($n)=>$n+1

Multiply items of first input (array of ints) by second input (int):

Anonymous function (closure) - 72 bytes - Try it online!

function($a,$n){return array_map(function($b)use($n){return$b*$n;},$a);}

Arrow function - 38 bytes - Try it online!

fn($a,$n)=>array_map(fn($b)=>$b*$n,$a)

Did you notice that $n is accessible in the inner function without a use $n statement? Yeah that is one of the arrow function features.


As a side note, I could not get arrow functions to work recursively (call the same arrow function inside itself), because we cannot give them a name and storing them as a closure in a variable like $f doesn't make $f accessible withing itself (sad). So this example doesn't work and using $f in first line causes a fatal error:

$f=fn($n)=>$n?$f($n-1):0;
$f(5); // Causes error: "PHP Notice: Undefined variable: f" + "PHP Fatal error: Uncaught Error: Function name must be a string"

But calling an arrow function withing a different arrow function works:

$f1=fn($n)=>$n+1;
$f2=fn($n)=>$f1($n-1);
$f1(2) // Returns 3
$f2(2) // Returns 2
added 50 characters in body
Source Link
Night2
  • 6k
  • 1
  • 14
  • 46

Arrow functions in PHP 7.4

PHP 7.4 is on RC2 version now and hopefully will be release in about 2 months. List of new features are here (this page can actually be updated when 7.4 is released). In 7.4, finally PHP has got the arrow functions, so not only function answers can be shorter now, but also passing closures to other functions can be a lot shorter too. Here are a few examples:

Return input + 1:

Anonymous function (closure) - 25 bytes - Try it online!

function($n){return$n+1;}

Arrow function - 12 bytes - Try it online!

fn($n)=>$n+1

Multiply items of first input (array of ints) by second input (int):

Anonymous function (closure) - 72 bytes - Try it online!

function($a,$n){return array_map(function($b)use($n){return$b*$n;},$a);}

Arrow function - 38 bytes - Try it online!

fn($a,$n)=>array_map(fn($b)=>$b*$n,$a)

Did you notice that $n is accessible in the inner function without a use $n statement? Yeah that is one of the arrow function features.


As a side note, I could not get arrow functions to work recursively (call the same arrow function inside itself), because we cannot give them a name and storing them as a closure in a variable like $f doesn't make $f accessible withing itself (sad). So this example doesn't work and using $f in first line causes a fatal error:

$f=fn($n)=>$n?$f($n-1):0;
$f(5); // Causes error: "PHP Notice: Undefined variable: f" + "PHP Fatal error: Uncaught Error: Function name must be a string"

But calling an arrow function withing a different arrow function works:

$f1=fn($n)=>$n+1;
$f2=fn($n)=>$f1($n-1);
$f1(2) // Returns 3
$f2(2) // Returns 2

Arrow functions in PHP 7.4

PHP 7.4 is on RC2 version now and hopefully will be release in about 2 months. List of new features are here (this page can actually be updated when 7.4 is released). In 7.4, finally PHP has got the arrow functions, so not only function answers can be shorter now, but also passing closures to other functions can be a lot shorter too. Here are a few examples:

Return input + 1:

Anonymous function (closure) - 25 bytes - Try it online!

function($n){return$n+1;}

Arrow function - 12 bytes - Try it online!

fn($n)=>$n+1

Multiply items of first input (array of ints) by second input (int):

Anonymous function (closure) - 72 bytes - Try it online!

function($a,$n){return array_map(function($b)use($n){return$b*$n;},$a);}

Arrow function - 38 bytes - Try it online!

fn($a,$n)=>array_map(fn($b)=>$b*$n,$a)

Did you notice that $n is accessible in the inner function without a use $n statement? Yeah that is one of the arrow function features.


As a side note, I could not get arrow functions to work recursively (call the same arrow function inside itself), because we cannot give them a name and storing them as a closure in a variable like $f doesn't make $f accessible withing itself (sad). So this example doesn't work:

$f=fn($n)=>$n?$f($n-1):0;
$f(5); // Causes error: "PHP Notice: Undefined variable: f" + "PHP Fatal error: Uncaught Error: Function name must be a string"

But calling an arrow function withing a different arrow function works:

$f1=fn($n)=>$n+1;
$f2=fn($n)=>$f1($n-1);
$f1(2) // Returns 3
$f2(2) // Returns 2

Arrow functions in PHP 7.4

PHP 7.4 is on RC2 version now and hopefully will be release in about 2 months. List of new features are here (this page can actually be updated when 7.4 is released). In 7.4, finally PHP has got the arrow functions, so not only function answers can be shorter now, but also passing closures to other functions can be a lot shorter too. Here are a few examples:

Return input + 1:

Anonymous function (closure) - 25 bytes - Try it online!

function($n){return$n+1;}

Arrow function - 12 bytes - Try it online!

fn($n)=>$n+1

Multiply items of first input (array of ints) by second input (int):

Anonymous function (closure) - 72 bytes - Try it online!

function($a,$n){return array_map(function($b)use($n){return$b*$n;},$a);}

Arrow function - 38 bytes - Try it online!

fn($a,$n)=>array_map(fn($b)=>$b*$n,$a)

Did you notice that $n is accessible in the inner function without a use $n statement? Yeah that is one of the arrow function features.


As a side note, I could not get arrow functions to work recursively (call the same arrow function inside itself), because we cannot give them a name and storing them as a closure in a variable like $f doesn't make $f accessible withing itself (sad). So this example doesn't work and using $f in first line causes a fatal error:

$f=fn($n)=>$n?$f($n-1):0;
$f(5); // Causes error: "PHP Notice: Undefined variable: f" + "PHP Fatal error: Uncaught Error: Function name must be a string"

But calling an arrow function withing a different arrow function works:

$f1=fn($n)=>$n+1;
$f2=fn($n)=>$f1($n-1);
$f1(2) // Returns 3
$f2(2) // Returns 2
Source Link
Night2
  • 6k
  • 1
  • 14
  • 46

Arrow functions in PHP 7.4

PHP 7.4 is on RC2 version now and hopefully will be release in about 2 months. List of new features are here (this page can actually be updated when 7.4 is released). In 7.4, finally PHP has got the arrow functions, so not only function answers can be shorter now, but also passing closures to other functions can be a lot shorter too. Here are a few examples:

Return input + 1:

Anonymous function (closure) - 25 bytes - Try it online!

function($n){return$n+1;}

Arrow function - 12 bytes - Try it online!

fn($n)=>$n+1

Multiply items of first input (array of ints) by second input (int):

Anonymous function (closure) - 72 bytes - Try it online!

function($a,$n){return array_map(function($b)use($n){return$b*$n;},$a);}

Arrow function - 38 bytes - Try it online!

fn($a,$n)=>array_map(fn($b)=>$b*$n,$a)

Did you notice that $n is accessible in the inner function without a use $n statement? Yeah that is one of the arrow function features.


As a side note, I could not get arrow functions to work recursively (call the same arrow function inside itself), because we cannot give them a name and storing them as a closure in a variable like $f doesn't make $f accessible withing itself (sad). So this example doesn't work:

$f=fn($n)=>$n?$f($n-1):0;
$f(5); // Causes error: "PHP Notice: Undefined variable: f" + "PHP Fatal error: Uncaught Error: Function name must be a string"

But calling an arrow function withing a different arrow function works:

$f1=fn($n)=>$n+1;
$f2=fn($n)=>$f1($n-1);
$f1(2) // Returns 3
$f2(2) // Returns 2