|
Mike - 05 Dec 2008
@TxtEdMacs: If you got a long if block with multiple else if statements, try using the break statement at the end of each in between the brackets at the end, to stop checking all other values.
Great tutorial. I wish I saw this before I learnt switch, as your explanation via example is great.
A PHP Soopr NOOB - 05 Dec 2008
Which would execute faster...
an if ifelse else,
or a case switch?
Personnaly I have to say this is an excellent tutorial...
Thanx!
A PHP Soopr NOOB - 05 Dec 2008
Which would execute faster...
an if ifelse else,
or a case switch?
Personnaly I have to say this is an excellent tutorial...
Thanx!
A PHP User - 05 Dec 2008
Ganesan Rajagopal is right - this code could look like this:
<?php
$Name = "Bob";
if ($Name == "Jim") print "Your name is Jim\n";
elseif ($Name == "Linda") print "Your name is Linda\n";
elseif ($Name == "Bob") print "Your name is Bob\n";
elseif ($Name == "Sally") print "Your name is Sally\n";
else print "I do not know your name!\n";
?>
i think that author should try a bit harder to find a good expamples ;] (but we all got the point anyway)
TxtEdMacs - 05 Dec 2008
PHP User - thanks for your response. My comment was really more of a complaint about the deficient implementation of the switch/case command compared to what I had used previously (albeit a long time ago). Moreover, until last night, I held the mistaken belief that the implementation in C was more robust. To my shock C and php switch/case works in an identical fashion. I gained my misimpression from an individual that "corrected" my FoxPro code and my incomplete understanding of the actual functioning of that code structure in C.
One more comment, if you check my comment associated with 2.6.8 and you will see I used the if () {...}elseif () {...} else } structure. However, I did not know that all subsequent conditions were checked after the first True. Since the code block was very large I could be taking a significant performance hit. Nonetheless, reflecting a bit more: on my machine it runs rapidly and the way the code is employed that may not be important. Regarding the latter point, the logs are analyzed off line for part of a statistical analysis of site usage.
Thanks again for your comment, I am a bit less ignorant today.
A PHP User - 05 Dec 2008
TxtEdMacs, Your'e looking for if-elseif.... not switch-case
eg:
if($foo == $bar){...}
elseif($foo == upcase($bar)) {...}
elseif{$foo == "help") {...}
else {...}
only here both sides of the test and the condition can be varied not just the right side...
TxtEdMacs - 05 Dec 2008
I wish this switch/case command would come near to matching the FoxPro code I used on DOS and Windows back in the 90's! The necessity of having the case be a specified value kills the utility in too many cases. It was nice too that the FP CASE didn't require the break; as the switch in C.
Hari - 05 Dec 2008
This is a cool way to have explained the case statement. It was simple and straight.
Ganesan Rajagopal - 05 Dec 2008
The nested if statements are a bit unfair. You would normall use a elseif statement for this type of usage. I admit that the nested makes the case statement look a lot more nicer, I think that it's better to let the case statement stand in it's own merit, rather than trying to make the if statement ugly.
effigy - 05 Dec 2008
The switch example can be tidied like so:
<?php
$Name = 'Bob';
switch($Name) {
case "Jim":
case "Linda":
case "Bob":
case "Sally":
print "Your name is $Name\n"; break;
default: print "I do not know your name!\n";
}
?>
Another PHP - 05 Dec 2008
Read Herbert Schildt`s C++ The Complete Reference. It`s more detailed
Shantuk123@lycos.com - 05 Dec 2008
This is an excellent explanation of case statements. Even better than other books and other languages have.
Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.
If you are reporting an error in the content, please tell me directly.
|