Skip to content

Instantly share code, notes, and snippets.

@jenovateurs
Last active December 1, 2019 11:00
Show Gist options
  • Save jenovateurs/8a33623661750a8993375ec5ebd0c442 to your computer and use it in GitHub Desktop.
Save jenovateurs/8a33623661750a8993375ec5ebd0c442 to your computer and use it in GitHub Desktop.
Nouveauté de PHP - Fonction anonyme simple
<?php
//Ancienne version de PHP
$nPiOld = 3.141;
$nValeursPiOld = array_map(function($nValeurOld) use($nPiOld){
return $nValeurOld * $nPiOld;
}, range(1,5));
print_r($nValeursPiOld);
// PHP 7.4
$nPiNew = 3.141;
$nValeursPiNew = array_map(fn($nValeurNew) => $nValeurNew * $nPiNew, range(1,5));
print_r($nValeursPiNew);
// ou
$nPiNew = 3.141;
$nValeursPiNew = array_map(fn(int $nValeurNew):float => $nValeurNew * $nPiNew, range(1,5));
print_r($nValeursPiNew);
/* Resultat
Array
(
[0] => 3.141
[1] => 6.282
[2] => 9.423
[3] => 12.564
[4] => 15.705
)
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment