| 
 Posted by lawrence k on 07/06/07 21:35 
I'm working in PHP 4. Even if a parameter is mandatory, I nearly 
always give it a default value of "false". Then I print an error 
message if someone who is calling my function forgot the mandatory 
parameter: 
 
function outputHtmlToShowFile($fileName=false) { 
  if ($fileName) { 
       // code goes here 
   } else { 
     echo "In outputHtmlToShowAFile() the code expected the first 
parameter to be a file name, but instead the value was false"; 
   } 
} 
 
How do others handle default parameter values? 
 
I've been avoiding this: 
 
function outputHtmlToShowFile($fileName) {  } 
 
because if I do this and someone forgets to pass a file name to the 
function, then the  PHP parser issues an error. I've been assuming 
that relying on the  PHP parser to write my error messages for me is 
unprofessional. Does anyone else feel that way?
 
  
Navigation:
[Reply to this message] 
 |