Re: [PHP] Switch statement Question
Hi, I have a code snippet here as in the following: //Switch statements between the four options switch($string) { case : $string= NOT book.author='All'; break; default: $string= $string . AND NOT book.author='All'; break; } This code does work, but I am wondering if it is possible in the switch statement clauses for me to do something like case does not equal to a certain author name if I don't want $string with that content to be processed. or, do I always use default in this case? Thanks in advance. Alice _ All-in-one security and maintenance for your PC. Get a free 90-day trial! http://www.windowsonecare.com/purchase/trial.aspx?sc_cid=wl_wlmail Well I will have to mention that switch becomes if after all internally so you could always use the if statement. I am a great fan of switch but since eclipse fail to format correctly embedded switch statements I am starting to use the old all good if() since it is the same thing. It is a matter of style actually although switch is slightly slower (so slightly that you can't notice in any case). I think there was a thread in this list for this comparison a few days ago. -- Thodoris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Switch statement Question
Hi, I have a code snippet here as in the following: //Switch statements between the four options switch($string) { case : $string= NOT book.author='All'; break; default: $string= $string . AND NOT book.author='All'; break; } This code does work, but I am wondering if it is possible in the switch statement clauses for me to do something like case does not equal to a certain author name if I don't want $string with that content to be processed. or, do I always use default in this case? Thanks in advance. Alice _ All-in-one security and maintenance for your PC. Get a free 90-day trial! http://www.windowsonecare.com/purchase/trial.aspx?sc_cid=wl_wlmail
RE: [PHP] Switch statement Question
-Original Message- From: Alice Wei [mailto:aj...@alumni.iu.edu] Sent: Thursday, January 29, 2009 3:02 PM To: php-general@lists.php.net Subject: [PHP] Switch statement Question Hi, I have a code snippet here as in the following: //Switch statements between the four options switch($string) { case : $string= NOT book.author='All'; break; default: $string= $string . AND NOT book.author='All'; break; } This code does work, but I am wondering if it is possible in the switch statement clauses for me to do something like case does not equal to a certain author name if I don't want $string with that content to be processed. or, do I always use default in this case? It's a bit non-conventional, but the switch block can be used like so: switch(true) { case (x y): dosomething(); break; case (y == 0): dosomethingelse(); break; default: somethingelseentirely(); break; } ...this way, your case statements can be expressions themselves, and it will always pick at least one of them to fire. HTH, // Todd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Switch statement Question
Boyd, Todd M. schreef: -Original Message- From: Alice Wei [mailto:aj...@alumni.iu.edu] Sent: Thursday, January 29, 2009 3:02 PM To: php-general@lists.php.net Subject: [PHP] Switch statement Question Hi, I have a code snippet here as in the following: //Switch statements between the four options switch($string) { case : $string= NOT book.author='All'; break; default: $string= $string . AND NOT book.author='All'; break; } This code does work, but I am wondering if it is possible in the switch statement clauses for me to do something like case does not equal to a certain author name if I don't want $string with that content to be processed. or, do I always use default in this case? It's a bit non-conventional, but the switch block can be used like so: switch(true) { case (x y): dosomething(); break; case (y == 0): dosomethingelse(); break; default: somethingelseentirely(); break; } some people really don't like this kind of thing (hi Robbert :-)), either way beware that the equality test is not strict, that is to say autocasting occurs (variable type doesn't have to match) an example: switch (true) { case 1: echo did you expect this?\n; // -- this is output break; case true: echo or this?\n; break; } ...this way, your case statements can be expressions themselves, and it will always pick at least one of them to fire. HTH, // Todd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] switch statement question
Say i have a variable $string_var = Once upon a time; is there a way to do this: Switch($string_var) { case(contains Once): doSomething(); break; case(contains the end.) doOtherThing(); break; case(contains time) doNothing(); break; default: echo ERROR } Thanks Alex -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] switch statement question
[snip] Say i have a variable $string_var = Once upon a time; is there a way to do this: Switch($string_var) { case(contains Once): doSomething(); break; case(contains the end.) doOtherThing(); break; case(contains time) doNothing(); break; default: echo ERROR } [/snip] http://www.php.net/manual/en/control-structures.switch.php Yes Virginia, there is a PHP function :^]. You can explode your $string_var into an array, loop through the array and test each word for a match. HTH! Jay -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch statement question
Alternatively Alexander, take a look at the string functions eg strstr(), stristr(), and strpos() all of which can be used to make a condition within the case() criteria. Regards, =dn [snip] Say i have a variable $string_var = Once upon a time; is there a way to do this: Switch($string_var) { case(contains Once): doSomething(); break; case(contains the end.) doOtherThing(); break; case(contains time) doNothing(); break; default: echo ERROR } [/snip] http://www.php.net/manual/en/control-structures.switch.php Yes Virginia, there is a PHP function :^]. You can explode your $string_var into an array, loop through the array and test each word for a match. HTH! Jay -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php