< PHP
Content added Content deleted
imported>mutante mNo edit summary |
imported>mutante mNo edit summary |
||
Line 3:
=== if / elseif ===
<pre>
if ($fnord == 23) {
echo "Found a 23!";
Line 12 ⟶ 13:
echo "Nothing special";
}
</pre>
* http://www.php.net/manual/en/control-structures.elseif.php
OR you can do the same like this
Line 18 ⟶ 21:
=== switch / case ===
<pre>
switch ($fnord) {
case 23:
Line 31 ⟶ 35:
echo "Nothing special";
}
</pre>
* http://www.php.net/switch
[http://www.php.net/switch read here why you need the "break"]
|
Revision as of 12:38, 8 March 2006
If you want to compare one variable to multiple values in PHP, you can do so 2 different ways:
if / elseif
if ($fnord == 23) { echo "Found a 23!"; } elseif ($fnord == 17) { echo "Found a 17!"; } elseif ($fnord == "Foobar") { echo "Fnord is a string!"; } else { echo "Nothing special"; }
OR you can do the same like this
switch / case
switch ($fnord) { case 23: echo "Found a 23!"; break; case 17: echo "Found a 17!"; break; case "Foobar": echo "Fnord is a string!"; break; default: echo "Nothing special"; }
read here why you need the "break"
Which is Faster ?
Now, you might wonder, which is "better" to use?
Look at this Benchmark provided by Shai-Tan comparing them.
It turns out that the difference is almost not noticable but switch is a tiny bit faster.
<@Shai-Tan> mutante: 0.00011 <@Shai-Tan> mutante: a moral victory at best