Skip to content

Instantly share code, notes, and snippets.

@inerba
Last active April 29, 2020 11:52
Show Gist options
  • Save inerba/8b9a0735783d87d89414360652ae29c7 to your computer and use it in GitHub Desktop.
Save inerba/8b9a0735783d87d89414360652ae29c7 to your computer and use it in GitHub Desktop.
[Funzioni] #appunti_php

Funzioni anonime e funzioni variavili

da usare usa e getta, non adatta per funzioni che vanno richiamate!

<?php

$somma = function($v1,$v2){
    echo $v1 + $v2;
};

$somma(3,5);

//Ritorna:: 8

$arr = [3,2];
array_walk($arr,function($val){
   echo $val*2;
});
//64

Spread operator

function spread(...$args){
    return $args;
}

print_r(spread(1,2,3));

//ritorna: Array ( [0] => 1 [1] => 2 [2] => 3 )

Se volessimo mandare un array

function spread(...$args){
    return $args;
}

$roba = [1,2,3];
print_r(spread($roba));
//ritorna: Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) )

print_r(spread(...$roba));
//ritorna: Array ( [0] => 1 [1] => 2 [2] => 3 )

dobbiamo comunque usare l'operatore ... che in questo caso scompone l'array nei singoli elementi, atrimenti ci troveremmo un array di array:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment