visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: Optional parameters in a user-defined function


By: alexatmuc U.S.A.  Date: 12/06/2003 00:00:00  English  Points: 33 Status: Answered
Quality : Excellent

Heya -

I was curious if its possible to write a function that has optional parameters. I will cite the php mail function as an example. The mail function takes three arguments by default (recipient,subject,body), but you can supply a fourth argument containing the email headers if you so desire.

Is this something easily done in a user-defined function? Do you have to do anything at all?

/* Alex */
By: VGR Date: 12/06/2003 03:56:00 English  Type : Answer
yes, of course. You just have to give them default values.

like this :

function foofunc($par1, &$par2,$debug=FALSE) {
if ($debug) //debug statements
} // foofunc

this I use to use functions and turn on debugging on-the-fly only in certain cases
By: alexatmuc Date: 12/06/2003 04:02:00 English  Type : Comment
I guess that thats just about as easy as I was expecting it to be ...

Thanks VGR!

/* Alex */
By: TheWebMonster Date: 12/06/2003 04:15:00 English  Type : Comment
one way of doing it is to pass an array to your function.
I'm sure their is a better way, but this is simple.

function XYZ($args){

$a = $args[param1];
$b = $args[param2];
$c = $args[param3];

/* Do your stuff here */

}

# you pass the params in an array

XYZ(array("param1"=>"qwerty1", "param2"=>"qwerty2", "param3"=>"qwerty3"));

Do register to be able to answer

EContact
browser fav
page generated in 296.949860 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page