Default Values for By-Reference Parameters

Unlike PHP 4, PHP 5 allows default values to be specified for parameters even when they are declared as by-reference:

  1.  
  2. function command_exists($command, &$output = null) {
  3.         $output = ‘whereis $cmd‘;
  4.         if (strpos($output, DIRECTORY_SEPARATOR) !== false) {
  5.                 return true;
  6.         } else {
  7.                 return false;
  8.         }
  9. }
  10.  

In the example above, the $output parameter is completely optional—if a variable is not passed in, a new one will be created within the context of command_exists() and, of
course, destroyed when the function returns.

Leave a Reply

Your email address will not be published. Required fields are marked *

*