Languages :: PHP :: Optional parameters in a user-defined function |
|||
| By: alexatmuc |
Date: 12/06/2003 00:00:00 |
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 | 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 | 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 | 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 |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








