note 77940 deleted from function.func-get-arg by danbrown
| From: | danbrown@php.net | Date: | Fri, 06 Feb 2015 21:35:08 +0000 |
| Subject: | note 77940 deleted from function.func-get-arg by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-201660@lists.php.net to get a copy of this message | ||
Note Submitter: wintermute
----
Bishop: Your ternary example is flawed:
<?php
// using ternary operators
function pick($a,$b) { return (isset($a) ? $a : $b); }
$a = (pick($b,$c) ? pick($c,$d) : null);
?>
If $b is not null, it will evaluate pick($c, $d) and return $c if that is not null. If $b and $c are
both null, it will not evaluate pick($c, $d) and will return null regardless of the value of $d.
I'm not sure how you'd save this.
Another option would be to use an array:
<?PHP
function pick($array)
{
foreach ($array as $element)
{
if ($element != null)
{
return($element)
}
}
return(null);
}
$a = pick(array($b, $c, $d, $e))
?>
I don't know that there's any real difference between this and the variable-argument form,
but some people might prefer it, so it's worth noting.