<?php// truevar_dump(null==null);var_dump(null=="");var_dump(null==0);var_dump(null==[]);// falsevar_dump(null==true);var_dump(null==2);$unknown=null;// checking for null: truevar_dump(null==$unknown);var_dump(is_null($unknown));// checking if not null: falsevar_dump(isset($unknown));// note, if strict type checking is applied, then one // cannot pass null (null not given a data type)functionsomeFunction($arg):void{if(null==$arg){echo"Null passed\n";}else{echo"Non-null passed: $arg\n";}}// all passing nullsomeFunction(null);someFunction([]);someFunction(0);someFunction("");// passing non-nullsomeFunction("null");
Union types
<?phpdeclare(strict_types=1);// define a function that can accept a // definite list of data typesfunctioncheckParamType(int|float|string$value):string{// match boolean "true" to one of // the following listed;// in this case it can only be one// of three since we are using union typesreturnmatch(true){is_int($value)=>"Integer value\n",is_float($value)=>"Float value\n",is_string($value)=>"String value\n",};}echocheckParamType(2);echocheckParamType("3");echocheckParamType(4.55);// this results in a fatal error//echo checkParamType(null);
Named functions
<?phpfunctionnamedParams(string$someName,bool$report):string{return$report?"Report name $someName\n":"Nothing to report\n";}echonamedParams("John Doe",true);echonamedParams("John Doe",false);// no change to the function implementation, // just declare argument identifiers in the callechonamedParams(report:false,someName:"John Doe");echonamedParams(report:true,someName:"John Doe");
Arrow functions (PHP 7.4+)
<?php// these are much like anonymous functions, with a shorter // in notation, suited for very simple, granular functions$shortFunc=fn(string$reply)=>"Reply : $reply";echo$shortFunc("Joe Bloggs");// passing the anonymous function to othersvar_dump($shortFunc("Joe Bloggs"));// unlike anonymous functions, arrow functions can use // external variables$anOuterVariable="Outside anon function";echo$shortFunc($anOuterVariable);var_dump($shortFunc($anOuterVariable));// using arrow functions as building blocks for higher-order // functions e.g. filterByValue(), which take other functions// as parameters or returns other functions$users=[['id'=>1,'name'=>'Joe','role'=>'USER'],['id'=>2,'name'=>'Jane','role'=>'ADMIN'],['id'=>3,'name'=>'Jim','role'=>'CLIENT'],];functionfilterByValue($key,$value){returnfn($input)=>$input[$key]==$value;}// $isAdmin is takes $input (in this case an array) and // finds data (elements) in which arrayName['role'] == 'ADMIN'$isAdmin=filterByValue("role","ADMIN");// array_filter takes each element from $users (i.e. an array)// and passes the elements of said array to isAdmin$admin=array_filter($users,$isAdmin);var_dump($admin);
Generator functions
<?phpfunctioncountDown(int$start):array{$result=[];for($i=$start;$i>0;$i--){// recall, this adds to the end of the arrayecho"Generating new random number...\n";$result[]=random_int(1,100);}return$result;}foreach(countDown(5)as$number){echo"Reporting random number...\n";echo$number."\n";}// $result resides in memory; to circumvent this,// the above can be rewritten as a generator function,functioncountDownGenerator(int$start):Generator{for($i=$start;$i>0;$i--){// recall, this adds to the end of the arrayecho"Generating new random number...\n";// at this stage, countDownGenerator() "returns"// the following before resuming, so $result is not neededyieldrandom_int(1,100);}// no return}/**
* Compared to the stepwise output, the following
* will print to the console:
*
* Generating new random number...
* Reporting generated random number...
* 2
* Generating new random number...
* Reporting generated random number...
* 16
*
* The randomly generated number is returns (yields)
* as defined before resuming with its logic. This is
* advantageous for very large datasets.
*/foreach(countDownGenerator(5)as$number){echo"Reporting generated random number...\n";echo$number."\n";}