On 16 February 2011 00:21, Mark Kelly <[email protected]> wrote:
> Hi.
>
> On Tuesday 15 Feb 2011 at 23:41 Andre Polykanine wrote:
>
>> Give it a default (possible empty) value:
>>
>> function MyFunction($x, $y, $z="") {
>> // function goes here
>> if (!empty($z)) {
>> // The optional parameter is given
>> }
>> }
>
> Using an empty string and the empty() function in this way can lead to subtle
> and hard to find bugs - for example if $z = 0, the code will not be executed.
> Note the list of things that are considered empty:
>
> http://uk.php.net/manual/en/function.empty.php
>
> Instead, consider setting the default value for $z to boolean false:
>
> function MyFunction ($x, $y, $z = FALSE) {
> if ($z) {
> // do stuff with $z
> }
> }
>
> In this way almost any value in $z will trigger the conditional code,
> including 0 or an empty string. The exceptions are FALSE and NULL. If you
> explicitly need to react to a NULL value, use is_null() to detect it.
>
> Cheers,
>
> Mark
You also have the option of variable arguments.
function foo($x, $y) // 2 mandatory arguments
{
print_r(func_get_args()); // Show all arguments.
}
func_get_args() will return an array of arguments to the function. All
of them, not just the declared ones.
--
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php