Re: [PHP] Switch Statement
Hello, I suggest you put default in that switch statement and var_dump the $_POST.That should be enough for a programmer to pin point what goes wrong. P:S **You might want to consider versioning your codes to go back into its history to see what has changed. Muhsin On 09/29/2013 04:33 AM, Ethan Rosenberg wrote: > Dear List - > > I have a working program. I made one change in a switch statement, > and it does not work. I'm probably missing something fundamental. > > Here are some code SNIPPETS... [please note that all my debug > statements are at the left margin] > > Setup... > > session_start(); > session_name("STORE"); > set_time_limit(2400); > ini_set('display_errors', 'on'); > ini_set('display_startup_errors', 'on'); > error_reporting(-2); > > ini_set('error_reporting', 'E_ALL | E_STRICT'); > ini_set('html_errors', 'On'); > ini_set('log_errors', 'On'); > require '/home/ethan/P/wk.inc'; //password file > $db = "Store"; > $cxn =mysqli_connect($host,$user,$password,$db); > if (!$cxn) > { > die('Connect Error (' . mysqli_connect_errno() . ') ' > . mysqli_connect_error()); > }// no error > if($_REQUEST['welcome_already_seen']!= "already_seen") > show_welcome(); > > //end setup > function show_welcome() //this is the input screen > { > > > echo " value='already_seen'>"; > echo " "; > > > } > > > //end input screen > > //Switch statement > > echo 'before'; > print_r($_POST); //post#1 > > switch ( $_POST['next_step'] ) > { > > case 'step20': > { > pint_r($_POST);//post#2 > echo 'step20'; > if(!empty($_POST['Cust_Num'])) > good(); > if(empty($_POST['Cust_Num'])) > bad(); > break; > } //end step20 > > > } //end switch > > > > post#1 > > beforeArray > ( > [Cust_Num] => 123 > [Fname] => > [Lname] => > [Street] => > [City] => > [state] => NY > [Zip] => 10952 > [PH1] => > [PH2] => > [PH3] => > [Date] => > [welcome_already_seen] => already_seen > [next_step] => step20 > > ) > > Cust_Num state and Zip are as entered. > > The switch statement is never entered, since post#2 is never > displayed, and neither good() or bad() functions are entered. > > > TIA > > Ethan > > > -- Extra details: OSS:Gentoo Linux profile:x86 Hardware:msi geforce 8600GT asus p5k-se location:/home/muhsin language(s):C/C++,PHP Typo:40WPM url:http://www.mzalendo.net url:http://www.zanbytes.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Switch Statement
What is the output? On Sun, Sep 29, 2013 at 1:34 AM, Ethan Rosenberg < erosenb...@hygeiabiomedical.com> wrote: > On 09/28/2013 10:53 PM, Aziz Saleh wrote: > >> Ethan, can you do a var_dump instead of print_r. It might be that >> next_step >> has spaces in it causing the switch to not match. >> >> Aziz >> >> >> > > Aziz - > > Used var_dump no further information > > > Ethan > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >
Re: [PHP] Switch Statement
On 09/28/2013 10:53 PM, Aziz Saleh wrote: Ethan, can you do a var_dump instead of print_r. It might be that next_step has spaces in it causing the switch to not match. Aziz Aziz - Used var_dump no further information Ethan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Switch Statement
Ethan, can you do a var_dump instead of print_r. It might be that next_step has spaces in it causing the switch to not match. Aziz On Sat, Sep 28, 2013 at 10:33 PM, Ethan Rosenberg < erosenb...@hygeiabiomedical.com> wrote: > Dear List - > > I have a working program. I made one change in a switch statement, and it > does not work. I'm probably missing something fundamental. > > Here are some code SNIPPETS... [please note that all my debug statements > are at the left margin] > > Setup... > > session_start(); > session_name("STORE"); > set_time_limit(2400); > ini_set('display_errors', 'on'); > ini_set('display_startup_**errors', 'on'); > error_reporting(-2); > > ini_set('error_reporting', 'E_ALL | E_STRICT'); > ini_set('html_errors', 'On'); > ini_set('log_errors', 'On'); > require '/home/ethan/P/wk.inc'; //password file > $db = "Store"; > $cxn =mysqli_connect($host,$user,$**password,$db); > if (!$cxn) > { > die('Connect Error (' . mysqli_connect_errno() . ') ' > . mysqli_connect_error()); > }// no error > if($_REQUEST['welcome_already_**seen']!= "already_seen") > > show_welcome(); > > //end setup > function show_welcome() //this is the input screen > { > > > echo " value='already_seen'>"; > echo " "; > > > } > > > //end input screen > > //Switch statement > > echo 'before'; > print_r($_POST); //post#1 > > switch ( $_POST['next_step'] ) > { > > case 'step20': > { > pint_r($_POST); //post#2 > echo 'step20'; > if(!empty($_POST['Cust_Num'])) > good(); > if(empty($_POST['Cust_Num'])) > bad(); > break; > } //end step20 > > > } //end switch > > > > post#1 > > beforeArray > ( > [Cust_Num] => 123 > [Fname] => > [Lname] => > [Street] => > [City] => > [state] => NY > [Zip] => 10952 > [PH1] => > [PH2] => > [PH3] => > [Date] => > [welcome_already_seen] => already_seen > [next_step] => step20 > > ) > > Cust_Num state and Zip are as entered. > > The switch statement is never entered, since post#2 is never displayed, > and neither good() or bad() functions are entered. > > > TIA > > Ethan > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >
[PHP] Switch Statement
Dear List - I have a working program. I made one change in a switch statement, and it does not work. I'm probably missing something fundamental. Here are some code SNIPPETS... [please note that all my debug statements are at the left margin] Setup... echo " value='already_seen'>"; echo " "; } //end input screen //Switch statement echo 'before'; print_r($_POST); //post#1 switch ( $_POST['next_step'] ) { case 'step20': { pint_r($_POST); //post#2 echo 'step20'; if(!empty($_POST['Cust_Num'])) good(); if(empty($_POST['Cust_Num'])) bad(); break; } //end step20 } //end switch post#1 beforeArray ( [Cust_Num] => 123 [Fname] => [Lname] => [Street] => [City] => [state] => NY [Zip] => 10952 [PH1] => [PH2] => [PH3] => [Date] => [welcome_already_seen] => already_seen [next_step] => step20 ) Cust_Num state and Zip are as entered. The switch statement is never entered, since post#2 is never displayed, and neither good() or bad() functions are entered. TIA Ethan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Switch - Case Statement Questions
On Nov 19, 2012, at 2:46 PM, Stuart Dallas wrote: > On 19 Nov 2012, at 19:35, Tim Streater wrote: > >> On 18 Nov 2012 at 14:44, Jim Giner wrote: >> Just so. Perhaps those who are not grasping the point could re-read their >> copy of "The Elements of Programming Style" by Kernighan and Plauger where >> this sort of issue is covered. > > And of course, nothing is allowed to have changed since 1978! Using a switch > in that manner is normal for me. Should I change my style simply because you > don't agree with it? If so, why don't you change your style because I don't > agree with that? Code is art; there is no "right" way to do it. Can code be > shown to be more efficient, elegant, faster, cleaner? Yes. Right or wrong? No. +1 for me as well. Publish data -- January 11, 1976??? This may be your father's book (or grand-father in my case). So, don't confuse me with old facts -- buy new ones. Just look to other languages (such as JAVA) for variations in switch. I think php got it right. After all, while(), if(), do/while() and even for() work around the concept of true -- why not switch? Cheers, tedd PS: We've had this conversation many years ago (but not as far back as 1976). _ t...@sperling.com http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] switch case madness
LOL, so true -Original Message- From: Adam Richardson [mailto:simples...@gmail.com] Sent: Wednesday, January 19, 2011 12:28 PM To: PHP-General Subject: Re: [PHP] switch case madness On Wed, Jan 19, 2011 at 12:44 PM, Micky Hulse wrote: > On Wed, Jan 19, 2011 at 6:45 AM, Joshua Kehn wrote: > > On Jan 19, 2011, at 9:43 AM, Jay Blanchard wrote: > >> [snip] > >> ... > >> [/snip] > >> Imagine when there'll be the day when you do not have to code at > >> all...just copy 'n paste snippets together in the order that you wish > >> them to work in and Voila'! - instant web app. > >> Th!! > > They have that. It's called Ruby on Rails. > > CodeIgniter and/or Django (Python) are fun. > > What about a middle of the road solution? > > Google for "php micro framework" and/or "python micro framework". > > I have yet to use a micro framework myself, but I am looking forward > to playing around with one for one of my next PHP projects... I would > love to find one that has a decent "micro" templating system. :D > > M > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > Here are some interesting frameworks you might want to check out: Lithium Fat-free Limonade Nephtali (my framework) fits within one file (except the config file). I'll send you a note off-list highlighting Nephtali's features. Adam -- Nephtali: A simple, flexible, fast, and security-focused PHP framework http://nephtaliproject.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case madness
On Wed, Jan 19, 2011 at 12:44 PM, Micky Hulse wrote: > On Wed, Jan 19, 2011 at 6:45 AM, Joshua Kehn wrote: > > On Jan 19, 2011, at 9:43 AM, Jay Blanchard wrote: > >> [snip] > >> ... > >> [/snip] > >> Imagine when there'll be the day when you do not have to code at > >> all...just copy 'n paste snippets together in the order that you wish > >> them to work in and Voila'! - instant web app. > >> Th!! > > They have that. It's called Ruby on Rails. > > CodeIgniter and/or Django (Python) are fun. > > What about a middle of the road solution? > > Google for "php micro framework" and/or "python micro framework". > > I have yet to use a micro framework myself, but I am looking forward > to playing around with one for one of my next PHP projects... I would > love to find one that has a decent "micro" templating system. :D > > M > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > Here are some interesting frameworks you might want to check out: Lithium Fat-free Limonade Nephtali (my framework) fits within one file (except the config file). I'll send you a note off-list highlighting Nephtali's features. Adam -- Nephtali: A simple, flexible, fast, and security-focused PHP framework http://nephtaliproject.com
Re: [PHP] switch case madness
On Jan 19, 2011, at 12:44 PM, Micky Hulse wrote: > On Wed, Jan 19, 2011 at 6:45 AM, Joshua Kehn wrote: >> They have that. It's called Ruby on Rails. > > CodeIgniter and/or Django (Python) are fun. > > What about a middle of the road solution? > > Google for "php micro framework" and/or "python micro framework". > > I have yet to use a micro framework myself, but I am looking forward > to playing around with one for one of my next PHP projects... I would > love to find one that has a decent "micro" templating system. :D > > M I love using CodeIgniter. I think it's the best minimalist PHP framework out there, and thankfully it doesn't pretend to be Rails. I've been told to look at Lithium but haven't gotten around to it yet. Regards, -Josh Joshua Kehn | josh.k...@gmail.com http://joshuakehn.com
Re: [PHP] switch case madness
On Wed, Jan 19, 2011 at 6:45 AM, Joshua Kehn wrote: > On Jan 19, 2011, at 9:43 AM, Jay Blanchard wrote: >> [snip] >> ... >> [/snip] >> Imagine when there'll be the day when you do not have to code at >> all...just copy 'n paste snippets together in the order that you wish >> them to work in and Voila'! - instant web app. >> Th!! > They have that. It's called Ruby on Rails. CodeIgniter and/or Django (Python) are fun. What about a middle of the road solution? Google for "php micro framework" and/or "python micro framework". I have yet to use a micro framework myself, but I am looking forward to playing around with one for one of my next PHP projects... I would love to find one that has a decent "micro" templating system. :D M -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case madness
. >> >> I do see from where it is you're coming, though, Josh --- once >> you've gotten the fundamentals, a lot of times it's easier - sometimes >> even a better idea - to use an existing, mature solution. What helps >> you to determine its value from a code standpoint? Your existing >> experience. >> >> -- >> >> Network Infrastructure Manager >> Documentation, Webmaster Teams >> http://www.php.net/ > > You are correct, and it is a shame to see many developers fall into the copy > / paste realm, especially with a language like PHP where such snippets are > often found easily but of dubious quality. Rolling your own is a great way to > understand how things work (or should work) internally, as well as giving you > valuable practice. I don't mean to discredit is. As I mentioned, more often > then not I'm a fan of it. > > Regards, > > -Josh > > Joshua Kehn | josh.k...@gmail.com > http://joshuakehn.com > > I find that at first, in any language, playing with the snippets through a form of stimuli and response(I move this, this happens, or doesn;t), helps to reinforce what I'm learning. But after understanding the snippet, I don't find that reapplying it later subverts the concept of being a 'real coder', because in the end you want to move to a more efficient means of coding, which is, if I'm not mistaken, where these larger frameworks come from-snippets that are reusable(all the way down to a login system snippet). -- It sure does get lonely up under this bridge. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case madness
> On Tue, Jan 18, 2011 at 23:39, Joshua Kehn wrote: >The use of existing packages is so increasingly prevalent that I > have the unfortunate displeasure of knowing many "developers" who do > nothing but this, yet who can't even answer simple questions about > general coding, and who cry and complain that a "previous developer" > must have borked something. I think Donovan is right on track here > --- he's just getting started, and challenging himself to learn the > language at a deeper level. That will make him a developer, not just > a copy-and-paster. > >I do see from where it is you're coming, though, Josh --- once > you've gotten the fundamentals, a lot of times it's easier - sometimes > even a better idea - to use an existing, mature solution. What helps > you to determine its value from a code standpoint? Your existing > experience. > > -- > > Network Infrastructure Manager > Documentation, Webmaster Teams > http://www.php.net/ You are correct, and it is a shame to see many developers fall into the copy / paste realm, especially with a language like PHP where such snippets are often found easily but of dubious quality. Rolling your own is a great way to understand how things work (or should work) internally, as well as giving you valuable practice. I don't mean to discredit is. As I mentioned, more often then not I'm a fan of it. Regards, -Josh Joshua Kehn | josh.k...@gmail.com http://joshuakehn.com
Re: [PHP] switch case madness
On Tue, Jan 18, 2011 at 23:39, Joshua Kehn wrote: > > Why not use one of the countless, not to mention secure and stable cookie > management systems available? If it's an exercise cool, I misunderstood. > > I'm not one to normally shun people rolling their own code, lord knows I've > done it more then once or twice, but there are some things I wouldn't touch > with a ten foot pool, and cookie management is one of them. The other would > be things like CSV parsers or text manipulations. The use of existing packages is so increasingly prevalent that I have the unfortunate displeasure of knowing many "developers" who do nothing but this, yet who can't even answer simple questions about general coding, and who cry and complain that a "previous developer" must have borked something. I think Donovan is right on track here --- he's just getting started, and challenging himself to learn the language at a deeper level. That will make him a developer, not just a copy-and-paster. I do see from where it is you're coming, though, Josh --- once you've gotten the fundamentals, a lot of times it's easier - sometimes even a better idea - to use an existing, mature solution. What helps you to determine its value from a code standpoint? Your existing experience. -- Network Infrastructure Manager Documentation, Webmaster Teams http://www.php.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case madness
> Imagine when there'll be the day when you do not have to code at > all...just copy 'n paste snippets together in the order that you wish > them to work in and Voila'! - instant web app. I have a Wordpress plugin that will do all of that for you. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case madness
On Jan 19, 2011, at 9:43 AM, Jay Blanchard wrote: > [snip] > ... > [/snip] > > Imagine when there'll be the day when you do not have to code at > all...just copy 'n paste snippets together in the order that you wish > them to work in and Voila'! - instant web app. > > > Th!! > They have that. It's called Ruby on Rails. Regards, -Josh Joshua Kehn | josh.k...@gmail.com http://joshuakehn.com
RE: [PHP] switch case madness
[snip] ... [/snip] Imagine when there'll be the day when you do not have to code at all...just copy 'n paste snippets together in the order that you wish them to work in and Voila'! - instant web app. Th!! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case madness
On Jan 18, 2011, at 11:53 PM, Donovan Brooke wrote: > The idea of using existing resources for efficiency is very valid indeed.. > especially with a job at hand. But, there are good reasons to > roll-your-own... education and knowing your own code are 2 that are important > to me right now. Besides, a cookie based log-in system is really not that > complex. ;-) > > Now.. payment gateway API? AJAX requests? I'll take the snippets please. > > Cheers, > Donovan (moving on to database administration) Payment gateways suck. AJAX requests are easy if you roll JavaScript well and have a decent inspector (Firebug). I agree, a simple login is not that complex. I thought it was a bit more involved then that. Regards, -Josh Joshua Kehn | josh.k...@gmail.com http://joshuakehn.com
Re: [PHP] switch case madness
On Tue, Jan 18, 2011 at 10:53:19PM -0600, Donovan Brooke wrote: > >Why not use one of the countless, not to mention secure and stable cookie > management systems available? If it's an exercise cool, I misunderstood. > > > >I'm not one to normally shun people rolling their own code, lord knows > I've done it more then once or twice, but there are some things I wouldn't > touch with a ten foot pool, and cookie management is one of them. The > other would be things like CSV parsers or text manipulations. > > > >Regards, > > > >-Josh > > > The idea of using existing resources for efficiency is very valid > indeed.. especially with a job at hand. But, there are good reasons to > roll-your-own... education and knowing your own code are 2 that are > important to me right now. Besides, a cookie based log-in system is > really not that complex. ;-) I have to agree here. Login systems aren't that hard to do. And finding a "library" or "toolkit" which does exactly what you want exactly the way you want may not be so easy. For example, I don't use MySQL unless I absolutely have to. But there aren't that many such systems based on PostgreSQL. I could take their MySQl code and hack it. But while analyzing their code, you find you could have done this yourself in half a day, less if you've done it before. I'm just thick enough to believe that most programming problems are relatively easily solved from scratch. And it's an interesting challenge to do so. Particularly when the result is something which can be reused in later projects. Yes, of course, I wouldn't touch payment gateways and the like. Paul -- Paul M. Foster http://noferblatz.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case madness
Why not use one of the countless, not to mention secure and stable cookie management systems available? If it's an exercise cool, I misunderstood. I'm not one to normally shun people rolling their own code, lord knows I've done it more then once or twice, but there are some things I wouldn't touch with a ten foot pool, and cookie management is one of them. The other would be things like CSV parsers or text manipulations. Regards, -Josh The idea of using existing resources for efficiency is very valid indeed.. especially with a job at hand. But, there are good reasons to roll-your-own... education and knowing your own code are 2 that are important to me right now. Besides, a cookie based log-in system is really not that complex. ;-) Now.. payment gateway API? AJAX requests? I'll take the snippets please. Cheers, Donovan (moving on to database administration) -- D Brooke -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case madness
On Jan 18, 2011, at 11:33 PM, Donovan Brooke wrote: >>> -- >>> D Brooke >> >> I just died a bit on the inside. >> >> Why would you build that from scratch? >> >> Regards, >> >> -Josh > > > Alright, I'll bite (since I affected you that much) ;-), > > do tell... > > Why not? Would you rather I use PHP's session_start()? > > > Donovan > Why not use one of the countless, not to mention secure and stable cookie management systems available? If it's an exercise cool, I misunderstood. I'm not one to normally shun people rolling their own code, lord knows I've done it more then once or twice, but there are some things I wouldn't touch with a ten foot pool, and cookie management is one of them. The other would be things like CSV parsers or text manipulations. Regards, -Josh Joshua Kehn | josh.k...@gmail.com http://joshuakehn.com
Re: [PHP] switch case madness
-- D Brooke I just died a bit on the inside. Why would you build that from scratch? Regards, -Josh Alright, I'll bite (since I affected you that much) ;-), do tell... Why not? Would you rather I use PHP's session_start()? Donovan -- D Brooke -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case madness
On Jan 18, 2011, at 11:01 PM, Donovan Brooke wrote: > Thanks. > > I had initialized $t_mssg as an empty string further up the chain out of old > habit.. removed that, and now it works... just built my first > basic cookie-based PHP/MySQL log-in script from scratch! ;-) > > Fun stuff, > Donovan > > > > -- > D Brooke I just died a bit on the inside. Why would you build that from scratch? Regards, -Josh Joshua Kehn | josh.k...@gmail.com http://joshuakehn.com
Re: [PHP] switch case madness
Thanks. I had initialized $t_mssg as an empty string further up the chain out of old habit.. removed that, and now it works... just built my first basic cookie-based PHP/MySQL log-in script from scratch! ;-) Fun stuff, Donovan -- D Brooke -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case madness
$t_mssg = 0 is different from $t_mssg = "0" (in all languages, as far as I know, maybe not in python, but I don't know python) On Tue, Jan 18, 2011 at 10:30 PM, Donovan Brooke wrote: > Hello, > > I must not understand PHP's switch/case.. > The case '0' below fires when $t_mssg = "" apparently. > Is this how it's suppose to work? I would think > it would only fire if it equaled "0". > > -- > print "-$t_mssg- "; > > if (isset($t_mssg)) { > switch ($t_mssg) { >case 0: > echo 'Log In Successful'; > break; > } > } > -- > > TIA, > Donovan > > -- > D Brooke > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Mujtaba
Re: [PHP] switch case madness
On Jan 18, 2011, at 10:30 PM, Donovan Brooke wrote: > Hello, > > I must not understand PHP's switch/case.. > The case '0' below fires when $t_mssg = "" apparently. > Is this how it's suppose to work? I would think > it would only fire if it equaled "0". > > -- > print "-$t_mssg- "; > > if (isset($t_mssg)) { > switch ($t_mssg) { >case 0: > echo 'Log In Successful'; > break; > } > } > -- > > TIA, > Donovan > > -- > D Brooke I use switch cases so rarely I would have to refer you to the documentation. Regards, -Josh Joshua Kehn | josh.k...@gmail.com http://joshuakehn.com
Re: [PHP] switch case madness
Hi, If it is a string "0", you should use: case "0". Because in PHP, 0 == false == null. You need to know the difference between == and ===. Regards, Dong Chen On Wed, Jan 19, 2011 at 2:30 PM, Donovan Brooke wrote: > Hello, > > I must not understand PHP's switch/case.. > The case '0' below fires when $t_mssg = "" apparently. > Is this how it's suppose to work? I would think > it would only fire if it equaled "0". > > -- > print "-$t_mssg- "; > > if (isset($t_mssg)) { > switch ($t_mssg) { >case 0: > echo 'Log In Successful'; > break; > } > } > -- > > TIA, > Donovan > > -- > D Brooke > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Regards, CHEN Dong
[PHP] switch case madness
Hello, I must not understand PHP's switch/case.. The case '0' below fires when $t_mssg = "" apparently. Is this how it's suppose to work? I would think it would only fire if it equaled "0". -- print "-$t_mssg- "; if (isset($t_mssg)) { switch ($t_mssg) { case 0: echo 'Log In Successful'; break; } } -- TIA, Donovan -- D Brooke -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Switch Question...
On Mon, 2010-11-15 at 22:43 +, Ashley Sheridan wrote: > On Mon, 2010-11-15 at 16:27 -0500, Steve Staples wrote: > > > Ok, dumb question, and i have tested, but I want to ensure that my tests > > were accurate, and behavior is correct. > > > > Ok, i have an integer, that based on what it is, does certain things... > > > > switch ((int)$intval) > > { > > } > > > > now, if i need to do something on a few of the numbers, then: > > case 0: > > # do nothing as strings will be (int) as 0 > > break; > > case 1: > > # do this > > break; > > case 12: > > # do this > > break; > > default: > > # do everything that is general to this > > > > which this works, as the $intval should never be anything that i am not > > expecting as there is another part of the code that decides which int's > > to send to this class. > > > > but is there a better way to do this? this could potentially do a lot > > more, and be more dynamic, but there is also the possibilty that i need > > to have more "custom" commands based on an integer value, and not > > something that is "generic". > > > > Is there a way to only do from like cases 2-11, without doing: > > case 2: > > case 3: > > case 4: > > . > > case 11: > > # do this stuff > > break; > > > > > > am I just overthinking things? i dunno... it's monday... brains still > > on weekend mode. > > > > Steve > > > > > > > There is actually a very cool way you can use a switch in PHP to do what > you want: > > switch(true) > { > case ($intval >= 2 && $intval <= 11): > { > // stuff here for cases 2-11 inclusive > break; > } > } > > Although I think in your case a series of if/else if statements would > work, and might possibly be more readable in your code. > > Thanks, > Ash > http://www.ashleysheridan.co.uk > > yeah, totally forgot about putting logic into the case... yep... still monday here... thanks ash! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Switch Question...
On Mon, 2010-11-15 at 16:27 -0500, Steve Staples wrote: > Ok, dumb question, and i have tested, but I want to ensure that my tests > were accurate, and behavior is correct. > > Ok, i have an integer, that based on what it is, does certain things... > > switch ((int)$intval) > { > } > > now, if i need to do something on a few of the numbers, then: > case 0: > # do nothing as strings will be (int) as 0 > break; > case 1: > # do this > break; > case 12: > # do this > break; > default: > # do everything that is general to this > > which this works, as the $intval should never be anything that i am not > expecting as there is another part of the code that decides which int's > to send to this class. > > but is there a better way to do this? this could potentially do a lot > more, and be more dynamic, but there is also the possibilty that i need > to have more "custom" commands based on an integer value, and not > something that is "generic". > > Is there a way to only do from like cases 2-11, without doing: > case 2: > case 3: > case 4: > . > case 11: > # do this stuff > break; > > > am I just overthinking things? i dunno... it's monday... brains still > on weekend mode. > > Steve > > There is actually a very cool way you can use a switch in PHP to do what you want: switch(true) { case ($intval >= 2 && $intval <= 11): { // stuff here for cases 2-11 inclusive break; } } Although I think in your case a series of if/else if statements would work, and might possibly be more readable in your code. Thanks, Ash http://www.ashleysheridan.co.uk
[PHP] Switch Question...
Ok, dumb question, and i have tested, but I want to ensure that my tests were accurate, and behavior is correct. Ok, i have an integer, that based on what it is, does certain things... switch ((int)$intval) { } now, if i need to do something on a few of the numbers, then: case 0: # do nothing as strings will be (int) as 0 break; case 1: # do this break; case 12: # do this break; default: # do everything that is general to this which this works, as the $intval should never be anything that i am not expecting as there is another part of the code that decides which int's to send to this class. but is there a better way to do this? this could potentially do a lot more, and be more dynamic, but there is also the possibilty that i need to have more "custom" commands based on an integer value, and not something that is "generic". Is there a way to only do from like cases 2-11, without doing: case 2: case 3: case 4: . case 11: # do this stuff break; am I just overthinking things? i dunno... it's monday... brains still on weekend mode. Steve -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
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
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
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
[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 vs elseif
At 2:18 PM -0600 1/13/09, Micah Gersten wrote: Jochem Maas wrote: switch (true) { case ($x === $y): // something > break; This is a misuse of the switch statement. Switch is meant to compare values to a single variable as stated on the manual page: http://us2.php.net/switch Thank you, Micah Gersten Micah: I disagree. You may use a switch statement as Jochem demonstrated -- I do it all the time. Just because you think the value within the switch is supposed to be the value examined doesn't mean that doing it otherwise is wrong -- it's just a different way of using that statement. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
I regret that I don't recall who made the brilliant observation that programmers spend the majority of their time *reading* code (their own or others) as opposed to *writing* code. So, I make it a point to try to make my code easily scannable. I only and always use the switch construct when evaluating the value of a single variable. The switch then tells me that at a glance when I'm reading code, and I don't have to plow through a bunch of elseif clauses to get the big picture of what the code is doing. The bottom line for me on this issue is the "bottom line" - productivity. Kirk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
On Tue, 2009-01-13 at 22:12 +0100, Jochem Maas wrote: > Robert Cummings schreef: > > On Tue, 2009-01-13 at 15:50 +0100, Jochem Maas wrote: > >> Ashley Sheridan schreef: > >>> On Mon, 2009-01-12 at 15:15 -0500, Frank Stanovcak wrote: > I've googled, and found some confusing answers. > I've tried searching the history of the news group, and only found info > on > switch or elseif seperately. :( > > Strictly from a performance stand point, not preference or anything > else, is > there a benefit of one over the other? > > > for($i=0;$i<3;$i++){ > switch($i){ > case 0: > header pg1 code > break; > case 1: > header pg2 code > break; > case 3: > header pg3 code > break; > }; > }; > > > or would that be better served using an if...elseif structure? > > Frank > > > > >>> And a switch is a lot neater for dealing with these sorts of things. I > >>> tend never to use if...elseif's at all, and use switches. Like Rob said, > >>> you can fall into further cases below, and it's very simple to add more > >>> at a later date. There is one place where an if...elseif would work and > >>> a switch could not, and that is where you were performing lots of > >>> different logic tests on different variables. > >> switch (true) { > >>case ($x === $y): > >>// something > >>break; > >> > >>case ($a != $b): > >>// something > >>break; > >> > >>case (myFunc()): > >>// something > >>break; > >> > >>case ($my->getChild()->hasEatenBeans()): > >>// something > >>break; > >> } > >> > >> evil ... but it works. > > > > What is your intent if both $x === $y and $a != $b? From the above > > example only the code for $x === y will execute. > > no shit. that would be the intention too :-) > > > This is hideous from a > > readability point of view since the first case and second case are > > matches, but only the first will get executed. This is better > > represented using an if/elseif/else structure to indicate the exclusion > > of later expressions. > > that is just a matter of opinion. it's no different to an > if/elseif/elseif/elseif > block functionally ... you just don't like the way it looks No, it's not just a matter of functionality in this case. It malforms the semantic meaning of the named expressions so that they no longer provide clearly readable meaning. > ... I'd point out though that you had no trouble determining the > code's intention correctly, and I'm sure you didn't stare at it too > long I'm special though ;) > ... so I'd hazard to say it's not *that* illegible. Yes, that's right, it's a hazard alright. A hazard to avoid by any self-respecting developer who's not competing in a code obfuscation contest :) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
Micah Gersten wrote: > Jochem Maas wrote: >> switch (true) { >> case ($x === $y): >> // something >> break; >> >> case ($a != $b): >> // something >> break; >> >> case (myFunc()): >> // something >> break; >> >> case ($my->getChild()->hasEatenBeans()): >> // something >> break; >> } >> >> evil ... but it works. >> >> >> >> > This is a misuse of the switch statement. Switch is meant to compare > values to a single variable as stated on the manual page: > http://us2.php.net/switch > > Thank you, > Micah Gersten > onShore Networks > Internal Developer > http://www.onshore.com > Actually, if you read the link you posted, the first paragraph: "In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for." Notice the (or expression), which I believe true is one. Also, down the page: "The case expression may be any expression that evaluates to a simple type, that is, integer or floating-point numbers and strings. Arrays or objects cannot be used here unless they are dereferenced to a simple type." I believe boolean is also one. I would point out however that the switch does a loose comparison, so this would be different: switch (true) { case (strpos("shawn", "s")): //actually returns 0 so is false break; case (strpos("shawn", "s") !== false): //returns true break; } -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
Nathan Rixham schreef: > Jochem Maas wrote: >> Micah Gersten schreef: >>> Jochem Maas wrote: switch (true) { > > should be switch(false) { > > :-) > it could be either depending on your needs, no? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
Robert Cummings schreef: > On Tue, 2009-01-13 at 15:50 +0100, Jochem Maas wrote: >> Ashley Sheridan schreef: >>> On Mon, 2009-01-12 at 15:15 -0500, Frank Stanovcak wrote: I've googled, and found some confusing answers. I've tried searching the history of the news group, and only found info on switch or elseif seperately. :( Strictly from a performance stand point, not preference or anything else, is there a benefit of one over the other? for($i=0;$i<3;$i++){ switch($i){ case 0: header pg1 code break; case 1: header pg2 code break; case 3: header pg3 code break; }; }; or would that be better served using an if...elseif structure? Frank >>> And a switch is a lot neater for dealing with these sorts of things. I >>> tend never to use if...elseif's at all, and use switches. Like Rob said, >>> you can fall into further cases below, and it's very simple to add more >>> at a later date. There is one place where an if...elseif would work and >>> a switch could not, and that is where you were performing lots of >>> different logic tests on different variables. >> switch (true) { >> case ($x === $y): >> // something >> break; >> >> case ($a != $b): >> // something >> break; >> >> case (myFunc()): >> // something >> break; >> >> case ($my->getChild()->hasEatenBeans()): >> // something >> break; >> } >> >> evil ... but it works. > > What is your intent if both $x === $y and $a != $b? From the above > example only the code for $x === y will execute. no shit. that would be the intention too :-) > This is hideous from a > readability point of view since the first case and second case are > matches, but only the first will get executed. This is better > represented using an if/elseif/else structure to indicate the exclusion > of later expressions. that is just a matter of opinion. it's no different to an if/elseif/elseif/elseif block functionally ... you just don't like the way it looks ... I'd point out though that you had no trouble determining the code's intention correctly, and I'm sure you didn't stare at it too long ... so I'd hazard to say it's not *that* illegible. > Cheers, > Rob. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
Jochem Maas wrote: Micah Gersten schreef: Jochem Maas wrote: switch (true) { should be switch(false) { :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
Micah Gersten schreef: > Jochem Maas wrote: >> switch (true) { >> case ($x === $y): >> // something >> break; >> >> case ($a != $b): >> // something >> break; >> >> case (myFunc()): >> // something >> break; >> >> case ($my->getChild()->hasEatenBeans()): >> // something >> break; >> } >> >> evil ... but it works. >> >> >> >> > This is a misuse of the switch statement. Switch is meant to compare > values to a single variable as stated on the manual page: > http://us2.php.net/switch bla bla, I did say it was evil, for a more thorough rebuttal a defer to Richard Lynch. > > Thank you, > Micah Gersten > onShore Networks > Internal Developer > http://www.onshore.com > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
I think if they didn't want us to use expressions in the case, then they wouldn't have put support into the language for that. I daresay you are reading more into the text than was intended... I certainly have found switch(true) with complex expressions for case quite handy and very clear code on several occasions. ymmv -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
Jochem Maas wrote: > switch (true) { > case ($x === $y): > // something > break; > > case ($a != $b): > // something > break; > > case (myFunc()): > // something > break; > > case ($my->getChild()->hasEatenBeans()): > // something > break; > } > > evil ... but it works. > > > > This is a misuse of the switch statement. Switch is meant to compare values to a single variable as stated on the manual page: http://us2.php.net/switch Thank you, Micah Gersten onShore Networks Internal Developer http://www.onshore.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
On Tue, 2009-01-13 at 15:50 +0100, Jochem Maas wrote: > Ashley Sheridan schreef: > > On Mon, 2009-01-12 at 15:15 -0500, Frank Stanovcak wrote: > >> I've googled, and found some confusing answers. > >> I've tried searching the history of the news group, and only found info on > >> switch or elseif seperately. :( > >> > >> Strictly from a performance stand point, not preference or anything else, > >> is > >> there a benefit of one over the other? > >> > >> > >> for($i=0;$i<3;$i++){ > >> switch($i){ > >> case 0: > >> header pg1 code > >> break; > >> case 1: > >> header pg2 code > >> break; > >> case 3: > >> header pg3 code > >> break; > >> }; > >> }; > >> > >> > >> or would that be better served using an if...elseif structure? > >> > >> Frank > >> > >> > >> > > And a switch is a lot neater for dealing with these sorts of things. I > > tend never to use if...elseif's at all, and use switches. Like Rob said, > > you can fall into further cases below, and it's very simple to add more > > at a later date. There is one place where an if...elseif would work and > > a switch could not, and that is where you were performing lots of > > different logic tests on different variables. > > switch (true) { > case ($x === $y): > // something > break; > > case ($a != $b): > // something > break; > > case (myFunc()): > // something > break; > > case ($my->getChild()->hasEatenBeans()): > // something > break; > } > > evil ... but it works. What is your intent if both $x === $y and $a != $b? From the above example only the code for $x === y will execute. This is hideous from a readability point of view since the first case and second case are matches, but only the first will get executed. This is better represented using an if/elseif/else structure to indicate the exclusion of later expressions. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
On Tue, Jan 13, 2009 at 9:50 AM, Jochem Maas wrote: > switch (true) { >case ($x === $y): >// something >break; > >case ($a != $b): >// something >break; > >case (myFunc()): >// something >break; > >case ($my->getChild()->hasEatenBeans()): >// something >break; > } > > evil ... but it works. > > PS - hi, people happy new year (or whatever) You too! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
Ashley Sheridan schreef: > On Mon, 2009-01-12 at 15:15 -0500, Frank Stanovcak wrote: >> I've googled, and found some confusing answers. >> I've tried searching the history of the news group, and only found info on >> switch or elseif seperately. :( >> >> Strictly from a performance stand point, not preference or anything else, is >> there a benefit of one over the other? >> >> >> for($i=0;$i<3;$i++){ >> switch($i){ >> case 0: >> header pg1 code >> break; >> case 1: >> header pg2 code >> break; >> case 3: >> header pg3 code >> break; >> }; >> }; >> >> >> or would that be better served using an if...elseif structure? >> >> Frank >> >> >> > And a switch is a lot neater for dealing with these sorts of things. I > tend never to use if...elseif's at all, and use switches. Like Rob said, > you can fall into further cases below, and it's very simple to add more > at a later date. There is one place where an if...elseif would work and > a switch could not, and that is where you were performing lots of > different logic tests on different variables. switch (true) { case ($x === $y): // something break; case ($a != $b): // something break; case (myFunc()): // something break; case ($my->getChild()->hasEatenBeans()): // something break; } evil ... but it works. PS - hi, people happy new year (or whatever) ... it's not that I'm dead ... I just can't be arsed atm. > Aside from that, I think > any speed benefit one would have over the other would be marginal. > > > Ash > www.ashleysheridan.co.uk > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
At 5:12 PM -0500 1/12/09, Robert Cummings wrote: On Mon, 2009-01-12 at 17:09 -0500, tedd wrote: At 4:57 PM -0500 1/12/09, Robert Cummings wrote: >No, that's DIFFERENT from what I wrote. Go back and re-read. > >:) I did and reread again -- it looks different. I'm simply talking about the spacing. No matter though, it's the same code. NO, it's not. Obviously my original comment has hit true in your case and it's not as readable as one might like to be self explanatory ;) Cheers, Rob. -- Okay. tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
On Mon, 2009-01-12 at 17:09 -0500, tedd wrote: > At 4:57 PM -0500 1/12/09, Robert Cummings wrote: > >No, that's DIFFERENT from what I wrote. Go back and re-read. > > > >:) > > I did and reread again -- it looks different. I'm simply talking > about the spacing. > > No matter though, it's the same code. NO, it's not. Obviously my original comment has hit true in your case and it's not as readable as one might like to be self explanatory ;) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
At 4:57 PM -0500 1/12/09, Robert Cummings wrote: No, that's DIFFERENT from what I wrote. Go back and re-read. :) I did and reread again -- it looks different. I'm simply talking about the spacing. No matter though, it's the same code. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
On Mon, 2009-01-12 at 16:49 -0500, tedd wrote: > At 3:32 PM -0500 1/12/09, Robert Cummings wrote: > >One has to wonder about the readability of the case version though since > >one may not notice immediately the missing break statement. > > > >Cheers, > >Rob. > > Yes, but that's because of the way you wrote it -- consider this: > > switch( $foo ) > { > case 0: > // something > break; > > case 2: > case 3: > // something else > // something elser > break; > > default: > // something defaulty > } > ?> > > If you group the case's together, then there's no problem > understanding what happens in case 2 or 3. > > Cheers, > > tedd > -- > --- > http://sperling.com http://ancientstones.com http://earthstones.com > I'd go further and enclose the code meant to execute in each case inside braces. Won't make a difference to the outcome of the code, but for some code editors (KATE for example), it helps to be able to close blocks of code so that it is hidden from view. DreamWeaver fans are missing out on this still I believe. Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
tedd wrote: At 3:32 PM -0500 1/12/09, Robert Cummings wrote: One has to wonder about the readability of the case version though since one may not notice immediately the missing break statement. Cheers, Rob. Yes, but that's because of the way you wrote it -- consider this: I'm sure that was completely intentional. If you group the case's together, then there's no problem understanding what happens in case 2 or 3. What happens if (like in Rob's example) I want a bunch of extra stuff to happen for case 2 but not 3? You can either group them like you have and have: case 2: case 3: if ($foo == 2) { // do extra stuff } ... break; or not put them under each other: case 2: // do extra stuff case 3: ... break; -- Postgresql & php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
On Mon, 2009-01-12 at 16:49 -0500, tedd wrote: > At 3:32 PM -0500 1/12/09, Robert Cummings wrote: > >One has to wonder about the readability of the case version though since > >one may not notice immediately the missing break statement. > > > >Cheers, > >Rob. > > Yes, but that's because of the way you wrote it -- consider this: > > switch( $foo ) > { > case 0: > // something > break; > > case 2: > case 3: > // something else > // something elser > break; > > default: > // something defaulty > } > ?> > > If you group the case's together, then there's no problem > understanding what happens in case 2 or 3. No, that's DIFFERENT from what I wrote. Go back and re-read. :) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
At 3:32 PM -0500 1/12/09, Robert Cummings wrote: One has to wonder about the readability of the case version though since one may not notice immediately the missing break statement. Cheers, Rob. Yes, but that's because of the way you wrote it -- consider this: If you group the case's together, then there's no problem understanding what happens in case 2 or 3. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
At 3:15 PM -0500 1/12/09, Frank Stanovcak wrote: I've googled, and found some confusing answers. I've tried searching the history of the news group, and only found info on switch or elseif seperately. :( Strictly from a performance stand point, not preference or anything else, is there a benefit of one over the other? for($i=0;$i<3;$i++){ switch($i){ case 0: header pg1 code break; case 1: header pg2 code break; case 3: header pg3 code break; }; }; or would that be better served using an if...elseif structure? Frank elseif!!! Arr !!! I guess that today is my day to get myself in all sorts of trouble. I have never used elseif -- and can't stand it, tedd's rules. if ($options > 2) { echo('use switch'); { else { echo('use if'); } Reason? It makes sense to me. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif -- switchtable
On Mon, Jan 12, 2009 at 4:00 PM, Daevid Vincent wrote: > On Mon, 2009-01-12 at 15:31 -0500, Eric Butera wrote: > >> On Mon, Jan 12, 2009 at 3:15 PM, Frank Stanovcak >> wrote: >> > I've googled, and found some confusing answers. >> > I've tried searching the history of the news group, and only found info on >> > switch or elseif seperately. :( >> > >> > Strictly from a performance stand point, not preference or anything else, >> > is >> > there a benefit of one over the other? >> > >> > >> > for($i=0;$i<3;$i++){ >> >switch($i){ >> >case 0: >> >header pg1 code >> >break; >> >case 1: >> >header pg2 code >> >break; >> >case 3: >> >header pg3 code >> >break; >> >}; >> > }; >> > >> > >> > or would that be better served using an if...elseif structure? >> > >> > Frank > > > >> This might be of interest in answering your question: >> >> http://www.suspekt.org/switchtable/ > > > How do I install this? The page and .tgz both give no instructions?! > > Are there any benchmarks to show speed comparisons? > > Also, is there any plans to have this patch/extension incorporated into > the real PHP trunk? > > Daevid. > http://daevid.com > Hi Daevid, Someone wrote a quick bench on this url: http://www.suspekt.org/2008/07/31/switch-table-extension/ As for all your other questions, you'll have to contact the author. I just thought it applied to this thread. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif -- switchtable
On Mon, 2009-01-12 at 15:31 -0500, Eric Butera wrote: > On Mon, Jan 12, 2009 at 3:15 PM, Frank Stanovcak > wrote: > > I've googled, and found some confusing answers. > > I've tried searching the history of the news group, and only found info on > > switch or elseif seperately. :( > > > > Strictly from a performance stand point, not preference or anything else, is > > there a benefit of one over the other? > > > > > > for($i=0;$i<3;$i++){ > >switch($i){ > >case 0: > >header pg1 code > >break; > >case 1: > >header pg2 code > >break; > >case 3: > >header pg3 code > >break; > >}; > > }; > > > > > > or would that be better served using an if...elseif structure? > > > > Frank > This might be of interest in answering your question: > > http://www.suspekt.org/switchtable/ How do I install this? The page and .tgz both give no instructions?! Are there any benchmarks to show speed comparisons? Also, is there any plans to have this patch/extension incorporated into the real PHP trunk? Daevid. http://daevid.com
Re: [PHP] switch vs elseif
On Mon, Jan 12, 2009 at 3:58 PM, Frank Stanovcak wrote: > > ""Eric Butera"" wrote in message > news:6a8639eb0901121231r253eed48xe1974d8ef44ab...@mail.gmail.com... >> On Mon, Jan 12, 2009 at 3:15 PM, Frank Stanovcak >> wrote: >>> I've googled, and found some confusing answers. >>> I've tried searching the history of the news group, and only found info >>> on >>> switch or elseif seperately. :( >>> >>> Strictly from a performance stand point, not preference or anything else, >>> is >>> there a benefit of one over the other? >>> >>> >>> for($i=0;$i<3;$i++){ >>>switch($i){ >>>case 0: >>>header pg1 code >>>break; >>>case 1: >>>header pg2 code >>>break; >>>case 3: >>>header pg3 code >>>break; >>>}; >>> }; >>> >>> >>> or would that be better served using an if...elseif structure? >>> >>> Frank >>> >>> >>> >>> -- >>> PHP General Mailing List (http://www.php.net/) >>> To unsubscribe, visit: http://www.php.net/unsub.php >>> >>> >> >> This might be of interest in answering your question: >> >> http://www.suspekt.org/switchtable/ > > Wow...so if I read that right. the only difference in the root code of PHP > is that the pre compiled code is easier to read. PHP actually generates the > If...elseif...elseif... structure any way when it compiles the script. > > ewww. > > Frank. > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > Yea I was bummed out to hear that too because I had heard switch was faster. Oh well. :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
""Eric Butera"" wrote in message news:6a8639eb0901121231r253eed48xe1974d8ef44ab...@mail.gmail.com... > On Mon, Jan 12, 2009 at 3:15 PM, Frank Stanovcak > wrote: >> I've googled, and found some confusing answers. >> I've tried searching the history of the news group, and only found info >> on >> switch or elseif seperately. :( >> >> Strictly from a performance stand point, not preference or anything else, >> is >> there a benefit of one over the other? >> >> >> for($i=0;$i<3;$i++){ >>switch($i){ >>case 0: >>header pg1 code >>break; >>case 1: >>header pg2 code >>break; >>case 3: >>header pg3 code >>break; >>}; >> }; >> >> >> or would that be better served using an if...elseif structure? >> >> Frank >> >> >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> > > This might be of interest in answering your question: > > http://www.suspekt.org/switchtable/ Wow...so if I read that right. the only difference in the root code of PHP is that the pre compiled code is easier to read. PHP actually generates the If...elseif...elseif... structure any way when it compiles the script. ewww. Frank. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
"Ashley Sheridan" wrote in message news:1231793310.3558.55.ca...@localhost.localdomain... > On Mon, 2009-01-12 at 15:15 -0500, Frank Stanovcak wrote: >> I've googled, and found some confusing answers. >> I've tried searching the history of the news group, and only found info >> on >> switch or elseif seperately. :( >> >> Strictly from a performance stand point, not preference or anything else, >> is >> there a benefit of one over the other? >> >> >> for($i=0;$i<3;$i++){ >> switch($i){ >> case 0: >> header pg1 code >> break; >> case 1: >> header pg2 code >> break; >> case 3: >> header pg3 code >> break; >> }; >> }; >> >> >> or would that be better served using an if...elseif structure? >> >> Frank >> >> >> > And a switch is a lot neater for dealing with these sorts of things. I > tend never to use if...elseif's at all, and use switches. Like Rob said, > you can fall into further cases below, and it's very simple to add more > at a later date. There is one place where an if...elseif would work and > a switch could not, and that is where you were performing lots of > different logic tests on different variables. Aside from that, I think > any speed benefit one would have over the other would be marginal. > > > Ash > www.ashleysheridan.co.uk > Yeah, I knew about the fall through benefit. :) I was just worried about speed since I have to loop through this several times and generate a pdf from it. Thanks folks! Frank -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
On Mon, 2009-01-12 at 15:15 -0500, Frank Stanovcak wrote: > I've googled, and found some confusing answers. > I've tried searching the history of the news group, and only found info on > switch or elseif seperately. :( > > Strictly from a performance stand point, not preference or anything else, is > there a benefit of one over the other? > > > for($i=0;$i<3;$i++){ > switch($i){ > case 0: > header pg1 code > break; > case 1: > header pg2 code > break; > case 3: > header pg3 code > break; > }; > }; > > > or would that be better served using an if...elseif structure? > > Frank > > > And a switch is a lot neater for dealing with these sorts of things. I tend never to use if...elseif's at all, and use switches. Like Rob said, you can fall into further cases below, and it's very simple to add more at a later date. There is one place where an if...elseif would work and a switch could not, and that is where you were performing lots of different logic tests on different variables. Aside from that, I think any speed benefit one would have over the other would be marginal. Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
On Mon, 2009-01-12 at 15:15 -0500, Frank Stanovcak wrote: > I've googled, and found some confusing answers. > I've tried searching the history of the news group, and only found info on > switch or elseif seperately. :( > > Strictly from a performance stand point, not preference or anything else, is > there a benefit of one over the other? > > > for($i=0;$i<3;$i++){ > switch($i){ > case 0: > header pg1 code > break; > case 1: > header pg2 code > break; > case 3: > header pg3 code > break; > }; > }; > > > or would that be better served using an if...elseif structure? In some caes you can use a switch statement to avoid redundant code by allowing a particular case to contain code for one condition then allowing fall through to the next condition's code. The following is a lame example: In the above exmaple case 2 runs the code within it's block AND the code within case 3's block. Using else you would probably do one of the following: Or with code redundancy: One has to wonder about the readability of the case version though since one may not notice immediately the missing break statement. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
On Mon, Jan 12, 2009 at 3:15 PM, Frank Stanovcak wrote: > I've googled, and found some confusing answers. > I've tried searching the history of the news group, and only found info on > switch or elseif seperately. :( > > Strictly from a performance stand point, not preference or anything else, is > there a benefit of one over the other? > > > for($i=0;$i<3;$i++){ >switch($i){ >case 0: >header pg1 code >break; >case 1: >header pg2 code >break; >case 3: >header pg3 code >break; >}; > }; > > > or would that be better served using an if...elseif structure? > > Frank > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > This might be of interest in answering your question: http://www.suspekt.org/switchtable/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] switch vs elseif
I've googled, and found some confusing answers. I've tried searching the history of the news group, and only found info on switch or elseif seperately. :( Strictly from a performance stand point, not preference or anything else, is there a benefit of one over the other? for($i=0;$i<3;$i++){ switch($i){ case 0: header pg1 code break; case 1: header pg2 code break; case 3: header pg3 code break; }; }; or would that be better served using an if...elseif structure? Frank -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] switch vs elseif
I've googled, and found some confusing answers. I've tried searching the history of the news group, and only found info on switch or elseif seperately. :( Strictly from a performance stand point, not preference or anything else, is there a benefit of one over the other? for($i=0;$i<3;$i++){ switch($i){ case 0: header pg1 code break; case 1: header pg2 code break; case 3: header pg3 code break; }; }; or would that be better served using an if...elseif structure? Frank -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Sep 13, 2008, at 12:12 PM, Robert Cummings wrote: On Sat, 2008-09-13 at 10:09 -0400, Eric Gorr wrote: On Sep 12, 2008, at 5:13 PM, Robert Cummings wrote: On Fri, 2008-09-12 at 16:51 -0400, Eric Gorr wrote: On Sep 12, 2008, at 4:27 PM, Robert Cummings wrote: On Fri, 2008-09-12 at 16:11 -0400, Eric Gorr wrote: On Sep 12, 2008, at 3:44 PM, Robert Cummings wrote: I don't see how that in any way makes an argument for or against. Once still must spend client's money wasting time on code that has questionable merit. Yes, some debugging code is a great boon in any application, but littered everywhere to fulfill someone's subjective philosophical ideal when sometimes it's just plain unnecessary... wasteful IMHO. As far as I know, no one has yet come up with a proof showing when debugging code is and/or is not necessary. The simple fact is that bugs can popup anywhere and spending a client's time and money by spending a few minutes writing all of the simple test cases throughout an application can be well worth it as it can save far more of the client's time and money by not wasting it on tracking down bugs that could have been easily caught. It is impractical to include debugging code for every conditional in a program. I have yet to see any evidence that it is impractical, especially after one has gotten into the habit. After all, for switch statements, adding in a default case takes mere seconds. Yes but if you do for case, you SHOULD do for if/else if/else which is an analagous approach. Doubly impractical to do so in PHP unless you have some way to prevent said debugging code from running in production. It isn't hard to prevent a code path from running in a production environment and allowing it to run in a development environment. Just one example, in PHP, would be globally defining something like PRODUCTION and then testing to see if it has a value of 1 or 0 and then writing an if statement to test the value before executing some code. There you go... you just ran a useless branch. Replacing one code path with another is hardly an optimial solution. What if your case statement is in a tight loop that runs a million times? How could that possibly matter since the code is never supposed to be executed to begin with and if it is executed it would immediately imply there is a bug? This discussion started because you said put a default statement in for debugging purposes rather than leave it empty. This suggests that you have a finite number of case statements that handle a specific set of values and that there may be values that don't need handling. Since they don't need handling the optimal path is not to have a default statement, but you suggest adding one with debugging information even though no processing need occur for some values. Now do you understand? Just because you have a switch doesn't mean all values need handling. Ah, while I had expected that my initial comments had been misinterpreted, I can see clearly now that they have. Hopefully, the messages the past couple of days have cleared things up. Furthermore, the whole point of these test cases is for those parts of the code which are never supposed to be executed to begin with, so that alone will aid in preventing said debugging code from executing in production...and if said debugging code does run in production, would that be such a bad thing (assuming it doesn't interfere with the user)? After all, because it (like the default switch case) was executed, it immediately implies there was a problem... If they're never supposed ot be executed then why are you adding extra code? That sounds like a need for better logic skills, not a need for debugging code. Because, it is never supposed to be ... not never will be. Bug's cause all kind of things to happen...including code paths that aren't supposed to happen. I doubt any client would believe it a good thing that a bug that should have been caught in development wasn't caught until production because mere minutes weren't spent putting in debug code that would have caught these bugs. You're probably one of those people that comments incrementing an index *shrug*. I mean it's mere seconds to add a useless chunk of comments that may someday help someone understand you're incrementing $i. While there is certainly a time and a place for comments which answer the questions of 'How?' or 'What?', the most useful comments nearly always answer the question of 'why?'. Why? The questions of 'How?' or 'What?' can almost always be easily determined with an analysis of the code. However, such an analysis does not always lead to understanding why code was written in the way it was written and I would strongly recommend people adopt such a comment style. Oh, for those using SCM (Source Code Management - like subversion), this policy applies to the checkin comments as well. Answering the question of w
Re: [PHP] switch case - to require the break statements seems strange to me
On Sat, 2008-09-13 at 10:09 -0400, Eric Gorr wrote: > On Sep 12, 2008, at 5:13 PM, Robert Cummings wrote: > > > On Fri, 2008-09-12 at 16:51 -0400, Eric Gorr wrote: > >> On Sep 12, 2008, at 4:27 PM, Robert Cummings wrote: > >> > >>> On Fri, 2008-09-12 at 16:11 -0400, Eric Gorr wrote: > On Sep 12, 2008, at 3:44 PM, Robert Cummings wrote: > > > > I don't see how that in any way makes an argument for or against. > > Once > > still must spend client's money wasting time on code that has > > questionable merit. Yes, some debugging code is a great boon in > > any > > application, but littered everywhere to fulfill someone's > > subjective > > philosophical ideal when sometimes it's just plain unnecessary... > > wasteful IMHO. > > As far as I know, no one has yet come up with a proof showing when > debugging code is and/or is not necessary. > > The simple fact is that bugs can popup anywhere and spending a > client's time and money by spending a few minutes writing all of > the > simple test cases throughout an application can be well worth it as > it > can save far more of the client's time and money by not wasting > it on > tracking down bugs that could have been easily caught. > >>> > >>> It is impractical to include debugging code for every conditional > >>> in a > >>> program. > >> > >> I have yet to see any evidence that it is impractical, especially > >> after one has gotten into the habit. After all, for switch > >> statements, > >> adding in a default case takes mere seconds. > > > > Yes but if you do for case, you SHOULD do for if/else if/else which is > > an analagous approach. > > > >>> Doubly impractical to do so in PHP unless you have some way to > >>> prevent said debugging code from running in production. > >> > >> It isn't hard to prevent a code path from running in a production > >> environment and allowing it to run in a development environment. Just > >> one example, in PHP, would be globally defining something like > >> PRODUCTION and then testing to see if it has a value of 1 or 0 and > >> then writing an if statement to test the value before executing some > >> code. > > > > There you go... you just ran a useless branch. Replacing one code path > > with another is hardly an optimial solution. What if your case > > statement > > is in a tight loop that runs a million times? > > How could that possibly matter since the code is never supposed to be > executed to begin with and if it is executed it would immediately > imply there is a bug? This discussion started because you said put a default statement in for debugging purposes rather than leave it empty. This suggests that you have a finite number of case statements that handle a specific set of values and that there may be values that don't need handling. Since they don't need handling the optimal path is not to have a default statement, but you suggest adding one with debugging information even though no processing need occur for some values. Now do you understand? Just because you have a switch doesn't mean all values need handling. > >> Furthermore, the whole point of these test cases is for those parts > >> of > >> the code which are never supposed to be executed to begin with, so > >> that alone will aid in preventing said debugging code from executing > >> in production...and if said debugging code does run in production, > >> would that be such a bad thing (assuming it doesn't interfere with > >> the > >> user)? After all, because it (like the default switch case) was > >> executed, it immediately implies there was a problem... > > > > If they're never supposed ot be executed then why are you adding extra > > code? That sounds like a need for better logic skills, not a need for > > debugging code. > > Because, it is never supposed to be ... not never will be. Bug's cause > all kind of things to happen...including code paths that aren't > supposed to happen. > > I doubt any client would believe it a good thing that a bug that > should have been caught in development wasn't caught until production > because mere minutes weren't spent putting in debug code that would > have caught these bugs. You're probably one of those people that comments incrementing an index *shrug*. I mean it's mere seconds to add a useless chunk of comments that may someday help someone understand you're incrementing $i. Similarly, for simplistic case statements, there's virtually no need for a block of debug code. > >>> Maybe you're > >>> confusing debugging code with unit tests. As I said earlier, it is > >>> far > >>> more practical to do so for complex conditions where a reader might > >>> easily get lost. Rather useless for simplistic cases. > >> > >> Until one finds it has saved hours because a problem was caught, I > >> can > >> understand why some would think that it is rather useless. > > > > I've spent hours
Re: [PHP] switch case - to require the break statements seems strange to me
On Sep 12, 2008, at 5:13 PM, Robert Cummings wrote: On Fri, 2008-09-12 at 16:51 -0400, Eric Gorr wrote: On Sep 12, 2008, at 4:27 PM, Robert Cummings wrote: On Fri, 2008-09-12 at 16:11 -0400, Eric Gorr wrote: On Sep 12, 2008, at 3:44 PM, Robert Cummings wrote: I don't see how that in any way makes an argument for or against. Once still must spend client's money wasting time on code that has questionable merit. Yes, some debugging code is a great boon in any application, but littered everywhere to fulfill someone's subjective philosophical ideal when sometimes it's just plain unnecessary... wasteful IMHO. As far as I know, no one has yet come up with a proof showing when debugging code is and/or is not necessary. The simple fact is that bugs can popup anywhere and spending a client's time and money by spending a few minutes writing all of the simple test cases throughout an application can be well worth it as it can save far more of the client's time and money by not wasting it on tracking down bugs that could have been easily caught. It is impractical to include debugging code for every conditional in a program. I have yet to see any evidence that it is impractical, especially after one has gotten into the habit. After all, for switch statements, adding in a default case takes mere seconds. Yes but if you do for case, you SHOULD do for if/else if/else which is an analagous approach. Doubly impractical to do so in PHP unless you have some way to prevent said debugging code from running in production. It isn't hard to prevent a code path from running in a production environment and allowing it to run in a development environment. Just one example, in PHP, would be globally defining something like PRODUCTION and then testing to see if it has a value of 1 or 0 and then writing an if statement to test the value before executing some code. There you go... you just ran a useless branch. Replacing one code path with another is hardly an optimial solution. What if your case statement is in a tight loop that runs a million times? How could that possibly matter since the code is never supposed to be executed to begin with and if it is executed it would immediately imply there is a bug? Furthermore, the whole point of these test cases is for those parts of the code which are never supposed to be executed to begin with, so that alone will aid in preventing said debugging code from executing in production...and if said debugging code does run in production, would that be such a bad thing (assuming it doesn't interfere with the user)? After all, because it (like the default switch case) was executed, it immediately implies there was a problem... If they're never supposed ot be executed then why are you adding extra code? That sounds like a need for better logic skills, not a need for debugging code. Because, it is never supposed to be ... not never will be. Bug's cause all kind of things to happen...including code paths that aren't supposed to happen. I doubt any client would believe it a good thing that a bug that should have been caught in development wasn't caught until production because mere minutes weren't spent putting in debug code that would have caught these bugs. Maybe you're confusing debugging code with unit tests. As I said earlier, it is far more practical to do so for complex conditions where a reader might easily get lost. Rather useless for simplistic cases. Until one finds it has saved hours because a problem was caught, I can understand why some would think that it is rather useless. I've spent hours on bugs before, they were never once related to not having put debugging fluff into a simple set of case statements. They were almost always related to lack of comments in a complex or hackish chunk of code. Great. I hope that continues. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
Well, I've often found the need to treat several conditions with the same set of statements within a switch: switch($some_number) { case 1: case 2: { // do some shizzle break; } case 3: { // foshizzle that nizzle break; } default: { // dizzle everything else here } } You never need a break in the last case, and you don't need a default case if you know all the values you expect, although if you do have one, I believe it does have to be the last statement. Ash www.ashleysheridan.co.uk --- Begin Message --- At 10:58 AM +0100 9/12/08, Luke wrote: I wonder if this is a shared trait between C and PHP (since I understand PHP is written in C) that the break; and the default: are placed for good practice in all switch statements since they prevent memory leaks? First, the evolution of computer languages from rocks to what we have now has produced numerous logic constructs. Those that are successful, remain and appear again and again in subsequent languages. Those that are not successful fade way. As a result, all languages are converging on a successful set logic constructs. The CASE statement has been one of those concepts that has been successful and I expect it to remain -- whereas, others like DO/WHILE may fade away -- I never found reason to use it. I may be wrong, I seem to remember that the CASE statement preceded the ELSE IF statement -- and is one of the reasons why I never use it (being dyslexic is another). If I was THE PHP czar, I would drop-kick ELSE IF -- for me it's perfectly useless and confusing. YMMV. Second, as for memory leaks??? The CASE statement, nor any other logic configuration, has anything to do with memory leaks -- that's different. I am sure there are people who will disagree, but a memory leak is a condition that comes about from declaring a memory allocation (a specific size) for a variable and then later upon discarding that variable and releasing its memory back into the free memory an error is made in its size. In other words, memory leaks are accounting errors in memory allocations that reduces the amount of available free memory OR worse yet, mistakenly assigns memory to new variables that is still in use and has not been released -- all of which can crash a program. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php --- End Message --- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Fri, 2008-09-12 at 16:51 -0400, Eric Gorr wrote: > On Sep 12, 2008, at 4:27 PM, Robert Cummings wrote: > > > On Fri, 2008-09-12 at 16:11 -0400, Eric Gorr wrote: > >> On Sep 12, 2008, at 3:44 PM, Robert Cummings wrote: > >>> > >>> I don't see how that in any way makes an argument for or against. > >>> Once > >>> still must spend client's money wasting time on code that has > >>> questionable merit. Yes, some debugging code is a great boon in any > >>> application, but littered everywhere to fulfill someone's subjective > >>> philosophical ideal when sometimes it's just plain unnecessary... > >>> wasteful IMHO. > >> > >> As far as I know, no one has yet come up with a proof showing when > >> debugging code is and/or is not necessary. > >> > >> The simple fact is that bugs can popup anywhere and spending a > >> client's time and money by spending a few minutes writing all of the > >> simple test cases throughout an application can be well worth it as > >> it > >> can save far more of the client's time and money by not wasting it on > >> tracking down bugs that could have been easily caught. > > > > It is impractical to include debugging code for every conditional in a > > program. > > I have yet to see any evidence that it is impractical, especially > after one has gotten into the habit. After all, for switch statements, > adding in a default case takes mere seconds. Yes but if you do for case, you SHOULD do for if/else if/else which is an analagous approach. > Now, for a large project that has already been written, it may be > impractical but only because it is unlikely anyone will be willing to > spend the time or money to go back and put the stuff back in... If they don't want to spend the money after the fact, they probably wouldn't want to spend the money before the fact. > > Doubly impractical to do so in PHP unless you have some way to > > prevent said debugging code from running in production. > > It isn't hard to prevent a code path from running in a production > environment and allowing it to run in a development environment. Just > one example, in PHP, would be globally defining something like > PRODUCTION and then testing to see if it has a value of 1 or 0 and > then writing an if statement to test the value before executing some > code. There you go... you just ran a useless branch. Replacing one code path with another is hardly an optimial solution. What if your case statement is in a tight loop that runs a million times? > Of course, there may be other clever solutions that aren't > popping into my head at the moment. I'm sure you could come up with > something better. I already did... don't add useless debugging code when the code is simple, easily understand, and highly unlikely to contain a bug. > Furthermore, the whole point of these test cases is for those parts of > the code which are never supposed to be executed to begin with, so > that alone will aid in preventing said debugging code from executing > in production...and if said debugging code does run in production, > would that be such a bad thing (assuming it doesn't interfere with the > user)? After all, because it (like the default switch case) was > executed, it immediately implies there was a problem... If they're never supposed ot be executed then why are you adding extra code? That sounds like a need for better logic skills, not a need for debugging code. > > Maybe you're > > confusing debugging code with unit tests. As I said earlier, it is far > > more practical to do so for complex conditions where a reader might > > easily get lost. Rather useless for simplistic cases. > > Until one finds it has saved hours because a problem was caught, I can > understand why some would think that it is rather useless. I've spent hours on bugs before, they were never once related to not having put debugging fluff into a simple set of case statements. They were almost always related to lack of comments in a complex or hackish chunk of code. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Sep 12, 2008, at 4:27 PM, Robert Cummings wrote: On Fri, 2008-09-12 at 16:11 -0400, Eric Gorr wrote: On Sep 12, 2008, at 3:44 PM, Robert Cummings wrote: I don't see how that in any way makes an argument for or against. Once still must spend client's money wasting time on code that has questionable merit. Yes, some debugging code is a great boon in any application, but littered everywhere to fulfill someone's subjective philosophical ideal when sometimes it's just plain unnecessary... wasteful IMHO. As far as I know, no one has yet come up with a proof showing when debugging code is and/or is not necessary. The simple fact is that bugs can popup anywhere and spending a client's time and money by spending a few minutes writing all of the simple test cases throughout an application can be well worth it as it can save far more of the client's time and money by not wasting it on tracking down bugs that could have been easily caught. It is impractical to include debugging code for every conditional in a program. I have yet to see any evidence that it is impractical, especially after one has gotten into the habit. After all, for switch statements, adding in a default case takes mere seconds. Now, for a large project that has already been written, it may be impractical but only because it is unlikely anyone will be willing to spend the time or money to go back and put the stuff back in... Doubly impractical to do so in PHP unless you have some way to prevent said debugging code from running in production. It isn't hard to prevent a code path from running in a production environment and allowing it to run in a development environment. Just one example, in PHP, would be globally defining something like PRODUCTION and then testing to see if it has a value of 1 or 0 and then writing an if statement to test the value before executing some code. Of course, there may be other clever solutions that aren't popping into my head at the moment. I'm sure you could come up with something better. Furthermore, the whole point of these test cases is for those parts of the code which are never supposed to be executed to begin with, so that alone will aid in preventing said debugging code from executing in production...and if said debugging code does run in production, would that be such a bad thing (assuming it doesn't interfere with the user)? After all, because it (like the default switch case) was executed, it immediately implies there was a problem... Maybe you're confusing debugging code with unit tests. As I said earlier, it is far more practical to do so for complex conditions where a reader might easily get lost. Rather useless for simplistic cases. Until one finds it has saved hours because a problem was caught, I can understand why some would think that it is rather useless. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Fri, 2008-09-12 at 16:11 -0400, Eric Gorr wrote: > On Sep 12, 2008, at 3:44 PM, Robert Cummings wrote: > > > > I don't see how that in any way makes an argument for or against. Once > > still must spend client's money wasting time on code that has > > questionable merit. Yes, some debugging code is a great boon in any > > application, but littered everywhere to fulfill someone's subjective > > philosophical ideal when sometimes it's just plain unnecessary... > > wasteful IMHO. > > As far as I know, no one has yet come up with a proof showing when > debugging code is and/or is not necessary. > > The simple fact is that bugs can popup anywhere and spending a > client's time and money by spending a few minutes writing all of the > simple test cases throughout an application can be well worth it as it > can save far more of the client's time and money by not wasting it on > tracking down bugs that could have been easily caught. It is impractical to include debugging code for every conditional in a program. Doubly impractical to do so in PHP unless you have some way to prevent said debugging code from running in production. Maybe you're confusing debugging code with unit tests. As I said earlier, it is far more practical to do so for complex conditions where a reader might easily get lost. Rather useless for simplistic cases. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Sep 12, 2008, at 3:44 PM, Robert Cummings wrote: I don't see how that in any way makes an argument for or against. Once still must spend client's money wasting time on code that has questionable merit. Yes, some debugging code is a great boon in any application, but littered everywhere to fulfill someone's subjective philosophical ideal when sometimes it's just plain unnecessary... wasteful IMHO. As far as I know, no one has yet come up with a proof showing when debugging code is and/or is not necessary. The simple fact is that bugs can popup anywhere and spending a client's time and money by spending a few minutes writing all of the simple test cases throughout an application can be well worth it as it can save far more of the client's time and money by not wasting it on tracking down bugs that could have been easily caught. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Fri, 2008-09-12 at 15:15 -0400, Eric Gorr wrote: > On Sep 12, 2008, at 2:51 PM, Robert Cummings wrote: > > > On Fri, 2008-09-12 at 14:33 -0400, Eric Gorr wrote: > >> On Sep 12, 2008, at 2:14 PM, Robert Cummings wrote: > >> > >>> On Fri, 2008-09-12 at 11:47 -0400, Eric Gorr wrote: > On Sep 12, 2008, at 11:39 AM, Andrew Ballard wrote: > > > On Fri, Sep 12, 2008 at 9:52 AM, Jochem Maas > > <[EMAIL PROTECTED]> > > wrote: > >> Luke schreef: > >>> > >>> I wonder if this is a shared trait between C and PHP (since I > >>> understand > >>> PHP > >>> is written in C) that the break; and the default: are placed for > >>> good > >>> practice in all switch statements since they prevent memory > >>> leaks? > >> > >> default is not required, never heard it was good practice to > >> always > >> put it > >> in. > > > > I can't say I've ever heard it recommended as good practice from > > the > > standpoint of performance in any specific language I've ever > > worked > > with, but I have heard people suggest that you always include an > > explicit default case in any kind of branching logic. It does seem > > useless to say > > > > default: // do nothing > > break; > > > > in a switch block, but I imagine the reasoning behind it is so > > that > > anyone who reads your code can see that you actually thought about > > what should/would happen if none of the other conditions were true > > rather than ignoring those conditions. > > It is always useful for a 'default:' case, which would normally do > nothing, to include some debug-only code so you can be notified if > the > default case is ever hit. Whenever I see an empty or just missing > 'default:' case, I always cringe. > >>> > >>> Whenever I see pointless debug fluff just for the sake of not having > >>> an > >>> empty or omitted default statement I cry. > >> > >> It's only pointless debug fluff until it saves untold numbers of > >> hours > >> which would have been spent attempting to track down a bug that would > >> have been caught by the now critical test which took only about 10 > >> seconds to write. > > > > Maybe for complex cases... but you used a broad brush when you > > advocated > > always doing this and so I most point out the pointlessness of debug > > fluff for simple use cases. > > Even in the simple cases, it is always useful to employ defensive > programming techniques because it may catch a bug which would have > otherwise taken hours to track down. > > > Since case statements are for the most part > > equivalent to if/else if/else statements, I must ask if you also > > enforce > > an else statement on all such conditionals. > > Who said anything about enforcement? > > I simply said it make me cringe and stated an opinion which is derived > from an accepted ideal that defensive programming is a good thing. I presumed that you wold attempt to prevent yourself from cringing and would thusly enforce such methodology upon yourself. I'm all for defensive programming where appropriate. I guess I just never realized some people need to defend against themselves at all times. > > Such debugging should be > > done on a case by case basis depending on the particular scenario at > > hand. If the code is simple and obvious then debugging serves little > > purpose but to convolute the code. > > "Convoluted" is a relative and subjective term. Quite true. > Once one gets used to writing and maintaining code written this way, > it is no longer convoluted. Furthermore, every decent editor these > days would allow one to collapse such code for those who find it > difficult to interpret...providing even a stronger reason to write it > in the first place. I don't see how that in any way makes an argument for or against. Once still must spend client's money wasting time on code that has questionable merit. Yes, some debugging code is a great boon in any application, but littered everywhere to fulfill someone's subjective philosophical ideal when sometimes it's just plain unnecessary... wasteful IMHO. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Sep 12, 2008, at 2:51 PM, Robert Cummings wrote: On Fri, 2008-09-12 at 14:33 -0400, Eric Gorr wrote: On Sep 12, 2008, at 2:14 PM, Robert Cummings wrote: On Fri, 2008-09-12 at 11:47 -0400, Eric Gorr wrote: On Sep 12, 2008, at 11:39 AM, Andrew Ballard wrote: On Fri, Sep 12, 2008 at 9:52 AM, Jochem Maas <[EMAIL PROTECTED]> wrote: Luke schreef: I wonder if this is a shared trait between C and PHP (since I understand PHP is written in C) that the break; and the default: are placed for good practice in all switch statements since they prevent memory leaks? default is not required, never heard it was good practice to always put it in. I can't say I've ever heard it recommended as good practice from the standpoint of performance in any specific language I've ever worked with, but I have heard people suggest that you always include an explicit default case in any kind of branching logic. It does seem useless to say default: // do nothing break; in a switch block, but I imagine the reasoning behind it is so that anyone who reads your code can see that you actually thought about what should/would happen if none of the other conditions were true rather than ignoring those conditions. It is always useful for a 'default:' case, which would normally do nothing, to include some debug-only code so you can be notified if the default case is ever hit. Whenever I see an empty or just missing 'default:' case, I always cringe. Whenever I see pointless debug fluff just for the sake of not having an empty or omitted default statement I cry. It's only pointless debug fluff until it saves untold numbers of hours which would have been spent attempting to track down a bug that would have been caught by the now critical test which took only about 10 seconds to write. Maybe for complex cases... but you used a broad brush when you advocated always doing this and so I most point out the pointlessness of debug fluff for simple use cases. Even in the simple cases, it is always useful to employ defensive programming techniques because it may catch a bug which would have otherwise taken hours to track down. Since case statements are for the most part equivalent to if/else if/else statements, I must ask if you also enforce an else statement on all such conditionals. Who said anything about enforcement? I simply said it make me cringe and stated an opinion which is derived from an accepted ideal that defensive programming is a good thing. Such debugging should be done on a case by case basis depending on the particular scenario at hand. If the code is simple and obvious then debugging serves little purpose but to convolute the code. "Convoluted" is a relative and subjective term. Once one gets used to writing and maintaining code written this way, it is no longer convoluted. Furthermore, every decent editor these days would allow one to collapse such code for those who find it difficult to interpret...providing even a stronger reason to write it in the first place. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Fri, 2008-09-12 at 14:33 -0400, Eric Gorr wrote: > On Sep 12, 2008, at 2:14 PM, Robert Cummings wrote: > > > On Fri, 2008-09-12 at 11:47 -0400, Eric Gorr wrote: > >> On Sep 12, 2008, at 11:39 AM, Andrew Ballard wrote: > >> > >>> On Fri, Sep 12, 2008 at 9:52 AM, Jochem Maas <[EMAIL PROTECTED]> > >>> wrote: > Luke schreef: > > > > I wonder if this is a shared trait between C and PHP (since I > > understand > > PHP > > is written in C) that the break; and the default: are placed for > > good > > practice in all switch statements since they prevent memory leaks? > > default is not required, never heard it was good practice to always > put it > in. > >>> > >>> I can't say I've ever heard it recommended as good practice from the > >>> standpoint of performance in any specific language I've ever worked > >>> with, but I have heard people suggest that you always include an > >>> explicit default case in any kind of branching logic. It does seem > >>> useless to say > >>> > >>> default: // do nothing > >>> break; > >>> > >>> in a switch block, but I imagine the reasoning behind it is so that > >>> anyone who reads your code can see that you actually thought about > >>> what should/would happen if none of the other conditions were true > >>> rather than ignoring those conditions. > >> > >> It is always useful for a 'default:' case, which would normally do > >> nothing, to include some debug-only code so you can be notified if > >> the > >> default case is ever hit. Whenever I see an empty or just missing > >> 'default:' case, I always cringe. > > > > Whenever I see pointless debug fluff just for the sake of not having > > an > > empty or omitted default statement I cry. > > It's only pointless debug fluff until it saves untold numbers of hours > which would have been spent attempting to track down a bug that would > have been caught by the now critical test which took only about 10 > seconds to write. Maybe for complex cases... but you used a broad brush when you advocated always doing this and so I most point out the pointlessness of debug fluff for simple use cases. Since case statements are for the most part equivalent to if/else if/else statements, I must ask if you also enforce an else statement on all such conditionals. Such debugging should be done on a case by case basis depending on the particular scenario at hand. If the code is simple and obvious then debugging serves little purpose but to convolute the code. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Sep 12, 2008, at 2:14 PM, Robert Cummings wrote: On Fri, 2008-09-12 at 11:47 -0400, Eric Gorr wrote: On Sep 12, 2008, at 11:39 AM, Andrew Ballard wrote: On Fri, Sep 12, 2008 at 9:52 AM, Jochem Maas <[EMAIL PROTECTED]> wrote: Luke schreef: I wonder if this is a shared trait between C and PHP (since I understand PHP is written in C) that the break; and the default: are placed for good practice in all switch statements since they prevent memory leaks? default is not required, never heard it was good practice to always put it in. I can't say I've ever heard it recommended as good practice from the standpoint of performance in any specific language I've ever worked with, but I have heard people suggest that you always include an explicit default case in any kind of branching logic. It does seem useless to say default: // do nothing break; in a switch block, but I imagine the reasoning behind it is so that anyone who reads your code can see that you actually thought about what should/would happen if none of the other conditions were true rather than ignoring those conditions. It is always useful for a 'default:' case, which would normally do nothing, to include some debug-only code so you can be notified if the default case is ever hit. Whenever I see an empty or just missing 'default:' case, I always cringe. Whenever I see pointless debug fluff just for the sake of not having an empty or omitted default statement I cry. It's only pointless debug fluff until it saves untold numbers of hours which would have been spent attempting to track down a bug that would have been caught by the now critical test which took only about 10 seconds to write. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
At 10:58 AM +0100 9/12/08, Luke wrote: I wonder if this is a shared trait between C and PHP (since I understand PHP is written in C) that the break; and the default: are placed for good practice in all switch statements since they prevent memory leaks? First, the evolution of computer languages from rocks to what we have now has produced numerous logic constructs. Those that are successful, remain and appear again and again in subsequent languages. Those that are not successful fade way. As a result, all languages are converging on a successful set logic constructs. The CASE statement has been one of those concepts that has been successful and I expect it to remain -- whereas, others like DO/WHILE may fade away -- I never found reason to use it. I may be wrong, I seem to remember that the CASE statement preceded the ELSE IF statement -- and is one of the reasons why I never use it (being dyslexic is another). If I was THE PHP czar, I would drop-kick ELSE IF -- for me it's perfectly useless and confusing. YMMV. Second, as for memory leaks??? The CASE statement, nor any other logic configuration, has anything to do with memory leaks -- that's different. I am sure there are people who will disagree, but a memory leak is a condition that comes about from declaring a memory allocation (a specific size) for a variable and then later upon discarding that variable and releasing its memory back into the free memory an error is made in its size. In other words, memory leaks are accounting errors in memory allocations that reduces the amount of available free memory OR worse yet, mistakenly assigns memory to new variables that is still in use and has not been released -- all of which can crash a program. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Fri, 2008-09-12 at 11:47 -0400, Eric Gorr wrote: > On Sep 12, 2008, at 11:39 AM, Andrew Ballard wrote: > > > On Fri, Sep 12, 2008 at 9:52 AM, Jochem Maas <[EMAIL PROTECTED]> > > wrote: > >> Luke schreef: > >>> > >>> I wonder if this is a shared trait between C and PHP (since I > >>> understand > >>> PHP > >>> is written in C) that the break; and the default: are placed for > >>> good > >>> practice in all switch statements since they prevent memory leaks? > >> > >> default is not required, never heard it was good practice to always > >> put it > >> in. > > > > I can't say I've ever heard it recommended as good practice from the > > standpoint of performance in any specific language I've ever worked > > with, but I have heard people suggest that you always include an > > explicit default case in any kind of branching logic. It does seem > > useless to say > > > >default: // do nothing > >break; > > > > in a switch block, but I imagine the reasoning behind it is so that > > anyone who reads your code can see that you actually thought about > > what should/would happen if none of the other conditions were true > > rather than ignoring those conditions. > > It is always useful for a 'default:' case, which would normally do > nothing, to include some debug-only code so you can be notified if the > default case is ever hit. Whenever I see an empty or just missing > 'default:' case, I always cringe. Whenever I see pointless debug fluff just for the sake of not having an empty or omitted default statement I cry. *begins weeping* Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Fri, 2008-09-12 at 10:58 +0100, Luke wrote: > I wonder if this is a shared trait between C and PHP (since I understand PHP > is written in C) that the break; and the default: are placed for good > practice in all switch statements since they prevent memory leaks? Prevent memory leaks? WHAT? Even if that were an issue, implementation of switch in PHP does not look like a switch. Breaks are used to prevent fall through to the next case condition. Default is used as a catch-all... if you need a catch-all. It is perfectly valid for a case statement to fall through to the next case statement if the code can be shared. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Sep 12, 2008, at 11:39 AM, Andrew Ballard wrote: On Fri, Sep 12, 2008 at 9:52 AM, Jochem Maas <[EMAIL PROTECTED]> wrote: Luke schreef: I wonder if this is a shared trait between C and PHP (since I understand PHP is written in C) that the break; and the default: are placed for good practice in all switch statements since they prevent memory leaks? default is not required, never heard it was good practice to always put it in. I can't say I've ever heard it recommended as good practice from the standpoint of performance in any specific language I've ever worked with, but I have heard people suggest that you always include an explicit default case in any kind of branching logic. It does seem useless to say default: // do nothing break; in a switch block, but I imagine the reasoning behind it is so that anyone who reads your code can see that you actually thought about what should/would happen if none of the other conditions were true rather than ignoring those conditions. It is always useful for a 'default:' case, which would normally do nothing, to include some debug-only code so you can be notified if the default case is ever hit. Whenever I see an empty or just missing 'default:' case, I always cringe. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Fri, Sep 12, 2008 at 9:52 AM, Jochem Maas <[EMAIL PROTECTED]> wrote: > Luke schreef: >> >> I wonder if this is a shared trait between C and PHP (since I understand >> PHP >> is written in C) that the break; and the default: are placed for good >> practice in all switch statements since they prevent memory leaks? > > default is not required, never heard it was good practice to always put it > in. I can't say I've ever heard it recommended as good practice from the standpoint of performance in any specific language I've ever worked with, but I have heard people suggest that you always include an explicit default case in any kind of branching logic. It does seem useless to say default: // do nothing break; in a switch block, but I imagine the reasoning behind it is so that anyone who reads your code can see that you actually thought about what should/would happen if none of the other conditions were true rather than ignoring those conditions. Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
Luke schreef: I wonder if this is a shared trait between C and PHP (since I understand PHP is written in C) that the break; and the default: are placed for good practice in all switch statements since they prevent memory leaks? default is not required, never heard it was good practice to always put it in. I assume that the fact that php is written in C has no baring on the functionality of php's switch implementation. *in theory* one should not have to be concerned with mem leaks when using php, that kind of low level stuff is the exact reason C isn't generally used to build web apps. in practice mem leaks can be an issue, but this is generally in long running cli scripts ... and I don't think breaks left out of switch statements have ever been shown to be the cause ... that said leaving out a break from a switch case may completely change the logic of the code, but that's a seperate issue (see tedd's explaination) 2008/9/10 Jochem Maas <[EMAIL PROTECTED]> tedd schreef: At 6:46 PM -0600 8/31/08, Govinda wrote: Not that it is an issue, but just to understand the logic- Why do we have to use 'break' statements in each case? switch ($i) { case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; } all 3 cases fire, even though $i only equals ONE of those case values (if I said that right). I mean if $i==1, then in other languages I don't expect the first or last case to fire! (?) Is the purpose just so one has the OPTION of letting them all fire, and turning that off with 'break'? Or is there a better reason? -G The "break" is to separate each case (i.e., condition) The switch ($i) isn't even needed if you do it like this: switch (true) { case $i==0: echo "i equals 0"; break; case $i==1: echo "i equals 1"; break; case $i==2: echo "i equals 2"; break; } this is 'true' ;-) and works very well when you want to check disparate truths but there are caveats: 1. it's less performant IIRC 2. there is no type checking, so auto-casting occurs during the test of each case's expression 3. it will become even less performant ... someone clever sod has a patch that heavily optimizes 'simple' switch statements ... see the internal mailing list archives for details (I can't remember the details) ... I gather this patch will eventually make it into the core, if it hasn't already. If you wanted to combine conditions, you could do this: switch (1) { case $i==-2: case $i==-1: case $i==0: echo "i is less than 0 but greater than -3 and is a counting number (i.e., no fraction)"; break; case $i==1: echo "i equals 1"; break; case $i==2: echo "i equals 2"; break; } Typed without checking and after my vacation. Cheers, tedd -- 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
Re: [PHP] switch case - to require the break statements seems strange to me
I wonder if this is a shared trait between C and PHP (since I understand PHP is written in C) that the break; and the default: are placed for good practice in all switch statements since they prevent memory leaks? 2008/9/10 Jochem Maas <[EMAIL PROTECTED]> > tedd schreef: > > At 6:46 PM -0600 8/31/08, Govinda wrote: >> >>> Not that it is an issue, but just to understand the logic- >>> Why do we have to use 'break' statements in each case? >>> >>> switch ($i) { >>> case 0: >>>echo "i equals 0"; >>>break; >>> case 1: >>>echo "i equals 1"; >>>break; >>> case 2: >>>echo "i equals 2"; >>>break; >>> } >>> >>> all 3 cases fire, even though $i only equals ONE of those case values (if >>> I said that right). >>> I mean if $i==1, then in other languages I don't expect the first or last >>> case to fire! (?) >>> Is the purpose just so one has the OPTION of letting them all fire, and >>> turning that off with 'break'? >>> Or is there a better reason? >>> >>> -G >>> >> >> >> The "break" is to separate each case (i.e., condition) >> >> The switch ($i) isn't even needed if you do it like this: >> >> switch (true) >> { >> case $i==0: >>echo "i equals 0"; >>break; >> >> case $i==1: >>echo "i equals 1"; >>break; >> >> case $i==2: >>echo "i equals 2"; >>break; >> } >> > > this is 'true' ;-) and works very well when you want to > check disparate truths but there are caveats: > > 1. it's less performant IIRC > 2. there is no type checking, so auto-casting occurs during the > test of each case's expression > 3. it will become even less performant ... someone clever sod has > a patch that heavily optimizes 'simple' switch statements ... see > the internal mailing list archives for details (I can't remember the > details) ... I gather this patch will eventually make it into the core, > if it hasn't already. > > >> If you wanted to combine conditions, you could do this: >> >> switch (1) >> { >> case $i==-2: >> case $i==-1: >> case $i==0: >> >>echo "i is less than 0 but greater than -3 and is a counting number >> (i.e., no fraction)"; >>break; >> >> case $i==1: >>echo "i equals 1"; >>break; >> >> case $i==2: >>echo "i equals 2"; >>break; >> } >> >> >> Typed without checking and after my vacation. >> >> Cheers, >> >> tedd >> >> > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Luke Slater
Re: [PHP] switch case - to require the break statements seems strange to me
tedd schreef: At 6:46 PM -0600 8/31/08, Govinda wrote: Not that it is an issue, but just to understand the logic- Why do we have to use 'break' statements in each case? switch ($i) { case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; } all 3 cases fire, even though $i only equals ONE of those case values (if I said that right). I mean if $i==1, then in other languages I don't expect the first or last case to fire! (?) Is the purpose just so one has the OPTION of letting them all fire, and turning that off with 'break'? Or is there a better reason? -G The "break" is to separate each case (i.e., condition) The switch ($i) isn't even needed if you do it like this: switch (true) { case $i==0: echo "i equals 0"; break; case $i==1: echo "i equals 1"; break; case $i==2: echo "i equals 2"; break; } this is 'true' ;-) and works very well when you want to check disparate truths but there are caveats: 1. it's less performant IIRC 2. there is no type checking, so auto-casting occurs during the test of each case's expression 3. it will become even less performant ... someone clever sod has a patch that heavily optimizes 'simple' switch statements ... see the internal mailing list archives for details (I can't remember the details) ... I gather this patch will eventually make it into the core, if it hasn't already. If you wanted to combine conditions, you could do this: switch (1) { case $i==-2: case $i==-1: case $i==0: echo "i is less than 0 but greater than -3 and is a counting number (i.e., no fraction)"; break; case $i==1: echo "i equals 1"; break; case $i==2: echo "i equals 2"; break; } Typed without checking and after my vacation. Cheers, tedd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
At 6:46 PM -0600 8/31/08, Govinda wrote: Not that it is an issue, but just to understand the logic- Why do we have to use 'break' statements in each case? switch ($i) { case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; } all 3 cases fire, even though $i only equals ONE of those case values (if I said that right). I mean if $i==1, then in other languages I don't expect the first or last case to fire! (?) Is the purpose just so one has the OPTION of letting them all fire, and turning that off with 'break'? Or is there a better reason? -G The "break" is to separate each case (i.e., condition) The switch ($i) isn't even needed if you do it like this: switch (true) { case $i==0: echo "i equals 0"; break; case $i==1: echo "i equals 1"; break; case $i==2: echo "i equals 2"; break; } If you wanted to combine conditions, you could do this: switch (1) { case $i==-2: case $i==-1: case $i==0: echo "i is less than 0 but greater than -3 and is a counting number (i.e., no fraction)"; break; case $i==1: echo "i equals 1"; break; case $i==2: echo "i equals 2"; break; } Typed without checking and after my vacation. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
Govinda schreef: Not that it is an issue, but just to understand the logic- Why do we have to use 'break' statements in each case? switch ($i) { case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; } all 3 cases fire, even though $i only equals ONE of those case values (if I said that right). I mean if $i==1, then in other languages I don't expect the first or last case to fire! (?) Is the purpose just so one has the OPTION of letting them all fire, and turning that off with 'break'? pretty much, all code will be run inside the switch after the first case found to equate (match) until a break is hit. try it: foreach (range(0,2) as $i) { echo "\$i = $i -- running switch ... \n"; switch ($i) { case 0: echo "first case\n"; case 1: echo "second case\n"; case 2: echo "third case\n"; } } Or is there a better reason? -G -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] switch case - to require the break statements seems strange to me
Not that it is an issue, but just to understand the logic- Why do we have to use 'break' statements in each case? switch ($i) { case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; } all 3 cases fire, even though $i only equals ONE of those case values (if I said that right). I mean if $i==1, then in other languages I don't expect the first or last case to fire! (?) Is the purpose just so one has the OPTION of letting them all fire, and turning that off with 'break'? Or is there a better reason? -G -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Switch To VoIPTelco And Save!
Hi, I want to introduce you to a new phone service that I'm using. It's called VoIPTelCo and it transforms your high speed Internet connection into a regular phone line. It works just like your current telephone service except it's 40% - 60% cheaper. You get features like Call Waiting, Caller ID Display, Voicemail, 3-Way calling, Call Forwarding and many more for a fraction of what you're paying now. You can even keep your existing number if you decide to switch to their service. With VoIPTelCo, I also save money on long distance calling. Unlike a regular phone line, VoIPTelCo is "portable", which means I can get phone service anywhere in the world. All I need is high speed Internet access. For example, I use my VoIPTelCo service when I travel and there are no long distance fees because these calls are considered "local". I save so much money just on this alone. Right now, VoIPTelCo is offering a ONE MONTH FREE trial period to friends and families of their customers. There is no obligation and it is completely free. If you'd like to try them out, just click on the link below and you will receive a 30 days trial period, free of charge: http://www.voiptelco.net Hope you'll find this useful.
Re: [PHP] switch()
> What you're saying in that case is either col_1 evaluates to value1 or > col_2 evaluates to value2. Correct. To be more specific (it was better I did from begining :)) After an "order" is submitted, the code is checking are there any coments about tax charges (col_1) and/or billing charges (col_2). If there is comment in col_1 send it by email to person in charge. If there is something in col_2 send it by email to (other) person in charge. I hope it does now make a sence. Sorry for this. > Unless you have some program logic to > prevent both from being true, you are comparing apples and oranges. And > any time col_1, evaluates to true, the "break" keyword will stop your > switch ladder. Remove break, and that won't happen. of course, if you > do that, then the question arises about why you are using a switch > ladder in the first place. > I forgot about this "break" (sounds crazy :)). Thanks for making this clear to me. -afan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch()
On Tuesday 29 August 2006 14:12, [EMAIL PROTECTED] wrote: > No. They are two if statements because they are indepedent. > > > On Tuesday 29 August 2006 13:44, Ray Hauge wrote: > >> Using a switch like this is more equivalent of > >> > >> if(something){ > >> // true > >> } else { > >> // false > >> } > > > > Actually, it's more like an if/elseif/else (if you use default:) or > > if/elseif > > (without default:) > > > > -- > > Ray Hauge > > Programmer/Systems Administrator > > American Student Loan Services > > www.americanstudentloan.com > > 1.800.575.1099 > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php I was unclear. Using a switch to replace an if statement would be replacing the if/elseif/else with the switch. You are right though. -- Ray Hauge Programmer/Systems Administrator American Student Loan Services www.americanstudentloan.com 1.800.575.1099 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch()
No. They are two if statements because they are indepedent. > On Tuesday 29 August 2006 13:44, Ray Hauge wrote: >> Using a switch like this is more equivalent of >> >> if(something){ >> // true >> } else { >> // false >> } > > Actually, it's more like an if/elseif/else (if you use default:) or > if/elseif > (without default:) > > -- > Ray Hauge > Programmer/Systems Administrator > American Student Loan Services > www.americanstudentloan.com > 1.800.575.1099 > > -- > 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
Re: [PHP] switch()
Means, my first solution was the only solution. Thanks. -afan > Using a switch like this is more equivalent of > > if(something){ > // true > } else { > // false > } > > You're doing : > > if(something1){ > // true 1 > } > > if(something2){ > // true2 > } > > hope that helps. > > -- > Ray Hauge > Programmer/Systems Administrator > American Student Loan Services > www.americanstudentloan.com > 1.800.575.1099 > > -- > 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
Re: [PHP] switch()
On Tuesday 29 August 2006 13:44, Ray Hauge wrote: > Using a switch like this is more equivalent of > > if(something){ > // true > } else { > // false > } Actually, it's more like an if/elseif/else (if you use default:) or if/elseif (without default:) -- Ray Hauge Programmer/Systems Administrator American Student Loan Services www.americanstudentloan.com 1.800.575.1099 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch()
On Tuesday 29 August 2006 13:31, [EMAIL PROTECTED] wrote: > I have something like this: > > $query = mysql_query(" > SELECT col_1, col_2 > FROM table > "); > $result = mysql_fetch_array($query); > if ($result['col_1'] == 'value_1') > { > // do something > } > > if ($result['col_2'] == 'value_2') > { > // do something > } > ?> > > when I tried to use swiitch() > switch (true) > { > case ($result['col_1'] == 'value_1'): > // do something > break; > > case ($result['col_2'] == 'value_2'): > // do something > break; > } > ?> > > it will give me only the first true case. if $result['col_1'] == 'value_1' > is true 2nd case will never be executed? > Does it mean I CANNOT use switch() in this case? > > -afan Using a switch like this is more equivalent of if(something){ // true } else { // false } You're doing : if(something1){ // true 1 } if(something2){ // true2 } hope that helps. -- Ray Hauge Programmer/Systems Administrator American Student Loan Services www.americanstudentloan.com 1.800.575.1099 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] switch()
I have something like this: when I tried to use swiitch() it will give me only the first true case. if $result['col_1'] == 'value_1' is true 2nd case will never be executed? Does it mean I CANNOT use switch() in this case? -afan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch it button
Ford, Mike wrote: How about something like: switch Beware that PHP_SELF is injectable like several other $_SERVER variables, so you must at least encode it to prevent XSS attacks. Eg. http://example.com/foo.php/";>
Re: [PHP] switch it button
At 12:44 PM +0100 8/23/06, Ross wrote: At the moment I have this, but I want to change it to one switch button I think I know what you mean -- a single switch button. Please review this kiddie-script: http://xn--ovg.com/a/toggle.php hth's tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] switch it button
On 23 August 2006 12:45, Ross wrote: > At the moment I have this, but I want to change it to one > switch button > > switch it. > I > > have tried but keep getting wrapped up in nested if-else statements. > Does anyone know how to make an efficient one button on-off switch. > > > > $session_switch = isset($_GET['session_switch']) ? > $_GET['session_switch'] > > 0; > if ($session_switch==1) { > echo "on"; > > > //do something here > > } > else { > > //do something else > > } > > > > off > on How about something like: switch 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: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] switch it button
[snip] At the moment I have this, but I want to change it to one switch button switch it. I have tried but keep getting wrapped up in nested if-else statements. Does anyone know how to make an efficient one button on-off switch. [/snip] Have you considered a switch statement? http://www.php.net/switch -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php