Last active
December 1, 2019 11:00
-
-
Save jenovateurs/8a33623661750a8993375ec5ebd0c442 to your computer and use it in GitHub Desktop.
Nouveauté de PHP - Fonction anonyme simple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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