RE: [PHP] Suggestion for "IN()"
| From: | Ford, Mike | Date: | Fri, 01 Oct 2004 13:46:40 +0000 |
| Subject: | RE: [PHP] Suggestion for "IN()" | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-198415@lists.php.net to get a copy of this message | ||
On 01 October 2004 14:05, Daniel Schierbeck wrote:
> Jay Blanchard wrote:>
> > You could write a function and share it with the rest of us!
>
> function between ($var, $min, $max)
> {
> return ($var > $min && $var < $max) ? TRUE : FALSE; }
Don't make PHP do more work than it needs to:
return ($var > $min && $var < $max)
is quite sufficient.
>
> function in ()
> {
> if (func_num_args() < 2)
> trigger_error('You must provide at least one
> argument for in()',
> E_USER_WARNING);
>
> $haystack = func_get_args();
> $needle = array_shift($haystack);
>
> return in_array($needle, $haystack) ? TRUE : FALSE; }
return in_array($needle, $haystack)
Also, you may find array_splice($haystack, 0, 1) more efficient, since
array_shift() renumbers all the keys. ($needle = $haystack[0]; unset
$haystack[0]; would be another possibility.)
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211