|
Posted by purcaholic on 05/19/07 19:10
On 19 Mai, 20:23, "Edward Z. Yang" <edwardzy...@thewritingpot.com>
wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Ciaran wrote:
> > All great ideas guys! Thanks for the replies on this. I especially
> > like purcaholic's one! Anyone know which of these methods would be the
> > fastest? Are any of them actually any faster than the basic one I
> > posted at the start?
>
> Purcaholic's one doesn't work (as it stands: he probably mean to use
> strpos) and is probably the slowest.
>
> In terms of speed, it's probably like this (from fastest to slowest):
>
> 1. Original method
> 2. isset($lookup[$var])
> 3. in_array
> 4. strstr
>
> If you have a particularly large set of numbers to compare against,
> method 2 may end up being faster due to the implementation of
> associative arrays needing only a binary search. I would use method 2
> simply because it's more maintainable (you can generate the $lookup
> array from anywhere you want).
>
> But I should warn you: this is really premature optimization and will
> make no difference at all most of the time.
>
> - --
> Edward Z. Yang GnuPG: 0x869C48DA
> HTML Purifier <htmlpurifier.org> Anti-XSS HTML Filter
> [[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]]
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.6 (MingW32)
> Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org
>
> iD8DBQFGT0CzqTO+fYacSNoRAt6ZAJ9stC/G1vPm0orjhfegDK6vRvsQhwCeNz9V
> iwhkovqnMTPpNIzUXH9Ff9I=
> =Qajq
> -----END PGP SIGNATURE-----
Sorry guys,
my example was wrong, but following should work:
if (strstr("|1|2|27|28|30|37|38|", "|".$var."|") !== false) {
echo "true";
}
Yes, this is not the best way, if there are much more numbers to
check, but a one line code for a simple check for a few numbers.
purcaolic
[Back to original message]
|