Re: [PHP] Re: Switch - Case Statement Questions
On 11/16/2012 8:33 PM, Iñigo Medina wrote: On Fri, 16 Nov 2012, Jim Giner wrote: On 11/16/2012 12:38 PM, Tim Streater wrote: On 16 Nov 2012 at 12:10, Omar Muhsin mrfroa...@gmail.com wrote: Hello, I was just wondering after writting the code in version 2 here below, it turns out in testing that it actually PHP is not validating the expressions instead always I get the first case. 1.Using nested if statement {THE INTENDED BEHAVIOR}: if ($count 14) $boxes = 3; elseif($count 7 $count = 14) You don't need the count=14 part as you will *only* get there if $count=14. $boxes = 2; else $boxes = 1; 2. Using Switch {ALWAYS FIRST CASE!!!} //$boxes = 1; //switch ($count) { //case ($count 14): //$boxes = 3; //break; //case ($count 7 $count = 14): //$boxes = 2; //break; //case ($count = 7): //default : //$boxes = 1; //break; //} Does anyone know the answer why using the Switch it always execute the first case ? As has been pointed out you need switch(true). That's a strange way of writing a switch, IMO. You should be using the if version. -- Cheers -- Tim Maybe I'm way out of touch, but when I look at php.net for the syntax of the switch statement I see: switch($var){ case (value): (do something) case (other value): (do something else) } IN your example, you are using a switch syntax that is nothing like that. I think that your case statements are not actually evaluating the value of $count, but are themselves evaluating to a true value that has nothing to do with $count. That `switch` is evaluating to true each case. It's an alternative of using if-elseif structure. iñ Here's the manual reference: http://us.php.net/manual/en/control-structures.switch.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php My point was that the OP is using a widely different syntax for a Switch statement, one that I don't understand at all. According to the Manual his statement should be: //switch ($count) { //case ( 14): //$boxes = 3; //break; //case ( 7): //$boxes = 2; //break; //default : //$boxes = 1; //break; //} To answer Iñigo's comment - the OP's version is very much like an If-ElseIF structure, and nothing like a Switch. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Switch - Case Statement Questions
On 18 Nov 2012 at 14:44, Jim Giner jim.gi...@albanyhandball.com wrote: 2. Using Switch {ALWAYS FIRST CASE!!!} //$boxes = 1; //switch ($count) { //case ($count 14): //$boxes = 3; //break; //case ($count 7 $count = 14): //$boxes = 2; //break; //case ($count = 7): //default : //$boxes = 1; //break; //} To answer Iñigo's comment - the OP's version is very much like an If-ElseIF structure, and nothing like a Switch. 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. See, it's all about expectations. When I worked at SLAC, there was a wise guy there who had removed the catalytic converter from his Scirocco and who though it clever to race up 280 from San Jose to SLAC at 100mph, sometimes slipstreaming behind another idiot doing the same. If I look in my rear-view mirror, I expect those I can see to be doing 70mph tops (that's what a lot of people did 30 years ago on 280), not 100, and to make judgements accordingly. Equally, I have certain expectations when I see a switch statement; it trying hard to look like if-elseif-etc is not one of them. -- Cheers -- Tim -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Switch - Case Statement Questions
On 19 Nov 2012, at 19:35, Tim Streater t...@clothears.org.uk wrote: On 18 Nov 2012 at 14:44, Jim Giner jim.gi...@albanyhandball.com wrote: 2. Using Switch {ALWAYS FIRST CASE!!!} //$boxes = 1; //switch ($count) { //case ($count 14): //$boxes = 3; //break; //case ($count 7 $count = 14): //$boxes = 2; //break; //case ($count = 7): //default : //$boxes = 1; //break; //} To answer Iñigo's comment - the OP's version is very much like an If-ElseIF structure, and nothing like a Switch. 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. See, it's all about expectations. When I worked at SLAC, there was a wise guy there who had removed the catalytic converter from his Scirocco and who though it clever to race up 280 from San Jose to SLAC at 100mph, sometimes slipstreaming behind another idiot doing the same. If I look in my rear-view mirror, I expect those I can see to be doing 70mph tops (that's what a lot of people did 30 years ago on 280), not 100, and to make judgements accordingly. Equally, I have certain expectations when I see a switch statement; it trying hard to look like if-elseif-etc is not one of them. Why is it their fault for doing 100mph and not yours for assuming they're only doing 70mph? I'm not saying they're right to be doing 100mph, but I'm definitely saying you're wrong for assuming they're only doing 70mph. Assumptions are the mother of all f*** ups. -Stuart -- Stuart Dallas 3ft9 Ltd http://3ft9.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Switch - Case Statement Questions
2012/11/19 Tim Streater t...@clothears.org.uk On 18 Nov 2012 at 14:44, Jim Giner jim.gi...@albanyhandball.com wrote: 2. Using Switch {ALWAYS FIRST CASE!!!} //$boxes = 1; //switch ($count) { //case ($count 14): //$boxes = 3; //break; //case ($count 7 $count = 14): //$boxes = 2; //break; //case ($count = 7): //default : //$boxes = 1; //break; //} To answer Iñigo's comment - the OP's version is very much like an If-ElseIF structure, and nothing like a Switch. 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. See, it's all about expectations. When I worked at SLAC, there was a wise guy there who had removed the catalytic converter from his Scirocco and who though it clever to race up 280 from San Jose to SLAC at 100mph, sometimes slipstreaming behind another idiot doing the same. If I look in my rear-view mirror, I expect those I can see to be doing 70mph tops (that's what a lot of people did 30 years ago on 280), not 100, and to make judgements accordingly. Equally, I have certain expectations when I see a switch statement; it trying hard to look like if-elseif-etc is not one of them. Just a minor rewrite of the original solution switch (true) { case $count = 7: $boxes = 1; break; case $count = 14: $boxes = 2; break; default: $boxes = 3; break; } Now -- the idea of switch as select on of some similar cases in mind: Is this really breaking any expectation? I think this one is a valid example, when switch(true) doesn't break the semantic of the switch-statement. And if this is already the end of expectations, then (sorry for that) the expectations are maybe slightly limited :X I don't say please use switch(true) wherever possible, but this one is nearly a prototype of a valid use-case: You have several cases all depending on a single state (the variable) and all of the same kind = constant. Beside, because it is so extremely simple example, it can become even more obvious switch (true) { case $count = 7: $boxes = 1; break; case $count = 14: $boxes = 2; break; default: $boxes = 3; break; } Guess some may argue about coding styles now, but thats not the questios here (and anyway: Even coding styles could/should be broken, if it leads to better code. Use it wisely ;)). And now: This _can_ break any expectation? By the way, your car-expectation sounds dangerous... -- Cheers -- Tim -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- github.com/KingCrunch
Re: [PHP] Re: Switch - Case Statement Questions
On Sat, Nov 17, 2012 at 4:13 PM, Sebastian Krebs krebs@gmail.com wrote: 2012/11/17 Andrew Ballard aball...@gmail.com On Nov 16, 2012 10:24 PM, tamouse mailing lists tamouse.li...@gmail.com wrote: On Fri, Nov 16, 2012 at 12:41 PM, Sebastian Krebs krebs@gmail.com wrote: Beside this it can be rewritten as switch ((int) (($count-1) / 7) { case 0: // 1-7 case 1: // 8 - 14 default: // above 15 } Nice code refactoring :) Just a tad obscure for someone coming along later Not only obscure, but depending on the rule being processed could be plain wrong. It works for the values shown so far, but what if another test is added to the use case in the future that doesn't fit the clever solution? Like so often in our developers world many things breaks, when it comes to new requirements. Without knowing the intent of the code, it could be a headache to maintain. Interesting, that you see 5 lines of code and assume, that nobody will ever get the intent of this code ;) Of course the context is missing. I guess, that $count is something like remaining days, or such, because 7 and 14 look like one week and two weeks, respectively. Thus I wouldn't name the variable $count [1], but $remainingDays and voila: Context is back and so is the intent :) Of course the above code has a quite limited use-case, but that was the requirement (for now, see KISS). If you need more, refactor/rewrite it. That's precisely my point. If the solution you posted, complete with your assumptions about the significance of the values 7 and 14, matches the requirements then use it. If your assumptions were wrong, and a future revision may potentially require special handling for certain values like 3 or 8 that cannot be evaluated using your equation, then the whole block may have to be refactored rather than simply adding a couple lines for the case. In the spirit of keeping it simple, I would prefer the code that requires less refactoring. I'm not assuming that nobody will ever get the [coder's] intent of a segment of code at all. It's not so much about what the code does, as WHY it is doing it. [1] Even further: I was taught, that if I ever want to name a variable like count, status, type, ... I should stop and probably don't use this name. In most cases they are used wrong. This example here is quite good: $count what? It depends. Generally I agree with you, but if it is a simple loop counter in a small block of code where variable scope issues and overall readability of the code is not diminished I'm fine with names like $count or $n. Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Switch - Case Statement Questions
On 17 Nov 2012 at 01:33, Iñigo Medina imed...@grosshat.com wrote: On Fri, 16 Nov 2012, Jim Giner wrote: Maybe I'm way out of touch, but when I look at php.net for the syntax of the switch statement I see: switch($var){ case (value): (do something) case (other value): (do something else) } IN your example, you are using a switch syntax that is nothing like that. I think that your case statements are not actually evaluating the value of $count, but are themselves evaluating to a true value that has nothing to do with $count. That `switch` is evaluating to true each case. It's an alternative of using if-elseif structure. It may be an alternative, but it breaks the principle of least surprise. If I'm looking through someone's code, and I see a switch, I expect to see it used as Jim described. If the writer needs a different type of logic to make a decision, that is what the cascaded if-elseif-elseif construct is for, when the tests don't fall into a simple set-of-values choice. The job of a programmer is not to be clever with a view to impress those who follow, but to achieve the desired outcome while at the same time making life easy for those who follow. -- Cheers -- Tim -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Switch - Case Statement Questions
On 17 Nov 2012, at 10:53, Tim Streater t...@clothears.org.uk wrote: On 17 Nov 2012 at 01:33, Iñigo Medina imed...@grosshat.com wrote: On Fri, 16 Nov 2012, Jim Giner wrote: Maybe I'm way out of touch, but when I look at php.net for the syntax of the switch statement I see: switch($var){ case (value): (do something) case (other value): (do something else) } IN your example, you are using a switch syntax that is nothing like that. I think that your case statements are not actually evaluating the value of $count, but are themselves evaluating to a true value that has nothing to do with $count. That `switch` is evaluating to true each case. It's an alternative of using if-elseif structure. It may be an alternative, but it breaks the principle of least surprise. If I'm looking through someone's code, and I see a switch, I expect to see it used as Jim described. If the writer needs a different type of logic to make a decision, that is what the cascaded if-elseif-elseif construct is for, when the tests don't fall into a simple set-of-values choice. I don't see how it breaks the principle of least surprise (which is a new one on me. KISS, yes. Least surprise? Wassat?). If you consider that behaviour to be surprising the don't ever go near Perl. A switch statement is basically saying take this value and compare it to each case below in the order they're presented until one matches, then execute that block of code until you hit a break which will jump execution out of the switch statement. I don't see how making this value true or false makes it any more surprising than if it's squirlookle. The job of a programmer is not to be clever with a view to impress those who follow, but to achieve the desired outcome while at the same time making life easy for those who follow. I don't consider that usage of the switch statement to be clever. It's a valid usage of the statement which gives it a lot more power. Specifically the ability to have the code for a given case deliberately fall through to the next case block is worth any price of admission. And finally, in my opinion, it's far cleaner than a succession of if/elseif/else statements, and ultimately gets compiled to very similar opcodes. I agree that the job of a programmer is to balance providing a fast, efficient, correct solution and minimising maintenance issues. I see neither side being compromised by using a switch statement in this manner, in fact I see both sides potentially getting a boost. Just because the usage is unfamiliar or even surprising to you doesn't mean it's wrong. -Stuart -- Stuart Dallas 3ft9 Ltd http://3ft9.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Switch - Case Statement Questions
On Nov 16, 2012 10:24 PM, tamouse mailing lists tamouse.li...@gmail.com wrote: On Fri, Nov 16, 2012 at 12:41 PM, Sebastian Krebs krebs@gmail.com wrote: Beside this it can be rewritten as switch ((int) (($count-1) / 7) { case 0: // 1-7 case 1: // 8 - 14 default: // above 15 } Nice code refactoring :) Just a tad obscure for someone coming along later Not only obscure, but depending on the rule being processed could be plain wrong. It works for the values shown so far, but what if another test is added to the use case in the future that doesn't fit the clever solution? Without knowing the intent of the code, it could be a headache to maintain. Just my 2 cents. Andrew
Re: [PHP] Re: Switch - Case Statement Questions
2012/11/17 Tim Streater t...@clothears.org.uk On 17 Nov 2012 at 01:33, Iñigo Medina imed...@grosshat.com wrote: On Fri, 16 Nov 2012, Jim Giner wrote: Maybe I'm way out of touch, but when I look at php.net for the syntax of the switch statement I see: switch($var){ case (value): (do something) case (other value): (do something else) } IN your example, you are using a switch syntax that is nothing like that. I think that your case statements are not actually evaluating the value of $count, but are themselves evaluating to a true value that has nothing to do with $count. That `switch` is evaluating to true each case. It's an alternative of using if-elseif structure. It may be an alternative, but it breaks the principle of least surprise. If I'm looking through someone's code, and I see a switch, I expect to see it used as Jim described. If the writer needs a different type of logic to make a decision, that is what the cascaded if-elseif-elseif construct is for, when the tests don't fall into a simple set-of-values choice. But switch(true) is not a surprise by itself, it becomes to, when the cases are surprising. And the given example directly fall into a simple set-of-values choice, but the problem is, that switch(val) by design only covers equality == and thats what this case is: A set like (1) between 1 and 7, (2) between 8 and 14, (Default) greater than 14. On the other side a looong if-elseif*-else-chain is often more confusing and needs refactoring anyway. (Beside: Was the principle of least surprise not a ruby-thing? ;)) The job of a programmer is not to be clever with a view to impress those who follow, but to achieve the desired outcome while at the same time making life easy for those who follow. I don't see a problem in case ($count = 7) or case ($count = 14) or case ($count 14)? I think a developer (or maybe programmer) doesn't need to be very clever to get the idea behind this. -- Cheers -- Tim -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- github.com/KingCrunch
Re: [PHP] Re: Switch - Case Statement Questions
2012/11/17 Andrew Ballard aball...@gmail.com On Nov 16, 2012 10:24 PM, tamouse mailing lists tamouse.li...@gmail.com wrote: On Fri, Nov 16, 2012 at 12:41 PM, Sebastian Krebs krebs@gmail.com wrote: Beside this it can be rewritten as switch ((int) (($count-1) / 7) { case 0: // 1-7 case 1: // 8 - 14 default: // above 15 } Nice code refactoring :) Just a tad obscure for someone coming along later Not only obscure, but depending on the rule being processed could be plain wrong. It works for the values shown so far, but what if another test is added to the use case in the future that doesn't fit the clever solution? Like so often in our developers world many things breaks, when it comes to new requirements. Without knowing the intent of the code, it could be a headache to maintain. Interesting, that you see 5 lines of code and assume, that nobody will ever get the intent of this code ;) Of course the context is missing. I guess, that $count is something like remaining days, or such, because 7 and 14 look like one week and two weeks, respectively. Thus I wouldn't name the variable $count [1], but $remainingDays and voila: Context is back and so is the intent :) Of course the above code has a quite limited use-case, but that was the requirement (for now, see KISS). If you need more, refactor/rewrite it. [1] Even further: I was taught, that if I ever want to name a variable like count, status, type, ... I should stop and probably don't use this name. In most cases they are used wrong. This example here is quite good: $count what? Just my 2 cents. Andrew -- github.com/KingCrunch
Re: [PHP] Re: Switch - Case Statement Questions
On Sat, Nov 17, 2012 at 3:13 PM, Sebastian Krebs krebs@gmail.com wrote: 2012/11/17 Andrew Ballard aball...@gmail.com On Nov 16, 2012 10:24 PM, tamouse mailing lists tamouse.li...@gmail.com wrote: Just a tad obscure for someone coming along later Without knowing the intent of the code, it could be a headache to maintain. Interesting, that you see 5 lines of code and assume, that nobody will ever get the intent of this code ;) Of course the context is missing. I guess, that $count is something like remaining days, or such, because 7 and 14 look like one week and two weeks, respectively. Thus I wouldn't name the variable $count [1], but $remainingDays and voila: Context is back and so is the intent :) This was actually the thrust of my remark about obscurity. I could easily see how the refactored algorithm worked from the original code's algorithm. What was obscure was exactly the variable name $count, i.e., the meaning of the data driving the algorithm. When you know the actual meaning of data being used, the obscurity goes away. Hence: useful variable names. Again, well done, Seb. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Switch - Case Statement Questions
On Sat, Nov 17, 2012 at 2:59 PM, Sebastian Krebs krebs@gmail.com wrote: (Beside: Was the principle of least surprise not a ruby-thing? ;)) No, but the Ruby (and Rails) world takes concepts like this and really runs with them. Principle of Least Astonishment has been around for quite some time, with examples known in software engineering, user interface design, and even real-world objects, It's been in the Patterns wiki for ages: http://c2.com/cgi/wiki?PrincipleOfLeastAstonishment . -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Switch - Case Statement Questions
On 16 Nov 2012 at 12:10, Omar Muhsin mrfroa...@gmail.com wrote: Hello, I was just wondering after writting the code in version 2 here below, it turns out in testing that it actually PHP is not validating the expressions instead always I get the first case. 1.Using nested if statement {THE INTENDED BEHAVIOR}: if ($count 14) $boxes = 3; elseif($count 7 $count = 14) You don't need the count=14 part as you will *only* get there if $count=14. $boxes = 2; else $boxes = 1; 2. Using Switch {ALWAYS FIRST CASE!!!} //$boxes = 1; //switch ($count) { //case ($count 14): //$boxes = 3; //break; //case ($count 7 $count = 14): //$boxes = 2; //break; //case ($count = 7): //default : //$boxes = 1; //break; //} Does anyone know the answer why using the Switch it always execute the first case ? As has been pointed out you need switch(true). That's a strange way of writing a switch, IMO. You should be using the if version. -- Cheers -- Tim -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Switch - Case Statement Questions
2012/11/16 Tim Streater t...@clothears.org.uk On 16 Nov 2012 at 12:10, Omar Muhsin mrfroa...@gmail.com wrote: Hello, I was just wondering after writting the code in version 2 here below, it turns out in testing that it actually PHP is not validating the expressions instead always I get the first case. 1.Using nested if statement {THE INTENDED BEHAVIOR}: if ($count 14) $boxes = 3; elseif($count 7 $count = 14) You don't need the count=14 part as you will *only* get there if $count=14. $boxes = 2; else $boxes = 1; 2. Using Switch {ALWAYS FIRST CASE!!!} //$boxes = 1; //switch ($count) { //case ($count 14): //$boxes = 3; //break; //case ($count 7 $count = 14): //$boxes = 2; //break; //case ($count = 7): //default : //$boxes = 1; //break; //} Does anyone know the answer why using the Switch it always execute the first case ? As has been pointed out you need switch(true). That's a strange way of writing a switch, IMO. You should be using the if version. Hi, I've seen long discussions about this ;) I think this one is a example, where 'switch(true)' is a legit expression, because switch-statements define a construct, where the interpreter should select one of multiple similar cases. This one seems legit, because they are very similar ;) It says In case of $count look like X do this, or $count look like Y do that, or $count look like Z do something else (and so on). For such I find the if-elseif-elseif-elseif-elseif-else-construction quite confusing. Beside this it can be rewritten as switch ((int) (($count-1) / 7) { case 0: // 1-7 case 1: // 8 - 14 default: // above 15 } Regards, Sebastian -- Cheers -- Tim -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- github.com/KingCrunch
[PHP] Re: Switch - Case Statement Questions
On 11/16/2012 12:38 PM, Tim Streater wrote: On 16 Nov 2012 at 12:10, Omar Muhsin mrfroa...@gmail.com wrote: Hello, I was just wondering after writting the code in version 2 here below, it turns out in testing that it actually PHP is not validating the expressions instead always I get the first case. 1.Using nested if statement {THE INTENDED BEHAVIOR}: if ($count 14) $boxes = 3; elseif($count 7 $count = 14) You don't need the count=14 part as you will *only* get there if $count=14. $boxes = 2; else $boxes = 1; 2. Using Switch {ALWAYS FIRST CASE!!!} //$boxes = 1; //switch ($count) { //case ($count 14): //$boxes = 3; //break; //case ($count 7 $count = 14): //$boxes = 2; //break; //case ($count = 7): //default : //$boxes = 1; //break; //} Does anyone know the answer why using the Switch it always execute the first case ? As has been pointed out you need switch(true). That's a strange way of writing a switch, IMO. You should be using the if version. -- Cheers -- Tim Maybe I'm way out of touch, but when I look at php.net for the syntax of the switch statement I see: switch($var){ case (value): (do something) case (other value): (do something else) } IN your example, you are using a switch syntax that is nothing like that. I think that your case statements are not actually evaluating the value of $count, but are themselves evaluating to a true value that has nothing to do with $count. Here's the manual reference: http://us.php.net/manual/en/control-structures.switch.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Switch - Case Statement Questions
On Fri, 16 Nov 2012, Jim Giner wrote: On 11/16/2012 12:38 PM, Tim Streater wrote: On 16 Nov 2012 at 12:10, Omar Muhsin mrfroa...@gmail.com wrote: Hello, I was just wondering after writting the code in version 2 here below, it turns out in testing that it actually PHP is not validating the expressions instead always I get the first case. 1.Using nested if statement {THE INTENDED BEHAVIOR}: if ($count 14) $boxes = 3; elseif($count 7 $count = 14) You don't need the count=14 part as you will *only* get there if $count=14. $boxes = 2; else $boxes = 1; 2. Using Switch {ALWAYS FIRST CASE!!!} //$boxes = 1; //switch ($count) { //case ($count 14): //$boxes = 3; //break; //case ($count 7 $count = 14): //$boxes = 2; //break; //case ($count = 7): //default : //$boxes = 1; //break; //} Does anyone know the answer why using the Switch it always execute the first case ? As has been pointed out you need switch(true). That's a strange way of writing a switch, IMO. You should be using the if version. -- Cheers -- Tim Maybe I'm way out of touch, but when I look at php.net for the syntax of the switch statement I see: switch($var){ case (value): (do something) case (other value): (do something else) } IN your example, you are using a switch syntax that is nothing like that. I think that your case statements are not actually evaluating the value of $count, but are themselves evaluating to a true value that has nothing to do with $count. That `switch` is evaluating to true each case. It's an alternative of using if-elseif structure. iñ Here's the manual reference: http://us.php.net/manual/en/control-structures.switch.php -- 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] Re: Switch - Case Statement Questions
On Fri, Nov 16, 2012 at 12:41 PM, Sebastian Krebs krebs@gmail.com wrote: Beside this it can be rewritten as switch ((int) (($count-1) / 7) { case 0: // 1-7 case 1: // 8 - 14 default: // above 15 } Nice code refactoring :) Just a tad obscure for someone coming along later, perhaps. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Cannot create statement handler
Thodoris wrote: I was wondering if anyone sees something I am bypassing: I have this sample code that works in another server: ?php function getClientFullName($dbh,$id){ // die(var_dump($dbh)); $sql = SELECT * FROM Clients WHERE Id=.$id; // die(print $sql); $sthr = $dbh-query($sql); // die(var_dump($sthr)); $res = $sthr-fetch(PDO::FETCH_ASSOC); return $res['Name']; } try { $dbh = new PDO('mysql:host=localhost;port=3306;dbname=ins', 'root', '', array(PDO::ATTR_PERSISTENT = false)); $sql = SELECT * FROM Contracts; $sth = $dbh-query($sql); print pre; while($res = $sth-fetch(PDO::FETCH_ASSOC)) { $name = getClientFullName($dbh,$res['ClientId']); print $name.br; } } catch (Exception $e) { print $e-getMessage(); } ? but in my case I can't make it work on my own server. I have removed both PHP and apache and compiled everything for source just to make sure that I will avoid possible problems using rpm. It seems that if I dump $sthr it returns false. Although if I print the query and use it directly it works fine. Apache 2.2.10 PHP 5.2.6 Linux EL5 I've posted this some days ago but none had any idea about this. Do think this could be a bug? obvious but: do a phpinfo() and check PDO is installed on the failing server check db is on the failing server check mysql is running on the failing server check username and pass are correct for db check username and pass combo can access via localhost -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Cannot create statement handler
Thodoris wrote: I was wondering if anyone sees something I am bypassing: I have this sample code that works in another server: ?php function getClientFullName($dbh,$id){ // die(var_dump($dbh)); $sql = SELECT * FROM Clients WHERE Id=.$id; // die(print $sql); $sthr = $dbh-query($sql); // die(var_dump($sthr)); $res = $sthr-fetch(PDO::FETCH_ASSOC); return $res['Name']; } try { $dbh = new PDO('mysql:host=localhost;port=3306;dbname=ins', 'root', '', array(PDO::ATTR_PERSISTENT = false)); $sql = SELECT * FROM Contracts; $sth = $dbh-query($sql); print pre; while($res = $sth-fetch(PDO::FETCH_ASSOC)) { $name = getClientFullName($dbh,$res['ClientId']); print $name.br; } } catch (Exception $e) { print $e-getMessage(); } ? but in my case I can't make it work on my own server. I have removed both PHP and apache and compiled everything for source just to make sure that I will avoid possible problems using rpm. It seems that if I dump $sthr it returns false. Although if I print the query and use it directly it works fine. Apache 2.2.10 PHP 5.2.6 Linux EL5 I've posted this some days ago but none had any idea about this. Do think this could be a bug? obvious but: do a phpinfo() and check PDO is installed on the failing server check db is on the failing server check mysql is running on the failing server check username and pass are correct for db check username and pass combo can access via localhost Yes all that are true because I am able to run a i simple query and retrieve the data normally. Not to mention that I double checked this. But when passing the database handler into a function then I get the results I mentioned before: Call to a member function fetch() on a non-object Nonetheless it is a good tip since someone can fail look in to the obvious. At last I have found what the problem was. It was more of a mysql configuration problem than PHP. I needed to activate MySQL's buffered queries that I was not aware that defaults to non-active in my configuration. I saw a relative bug that suggested this so the only thing that I should do was: $dbh = new PDO('mysql:host=localhost;port=3306;dbname=xxx', 'xxx', 'xxx', array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY=true)); So I probably got stuck to the *obvious*. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Cannot create statement handler
Thodoris wrote: I was wondering if anyone sees something I am bypassing: I have this sample code that works in another server: ?php function getClientFullName($dbh,$id){ // die(var_dump($dbh)); $sql = SELECT * FROM Clients WHERE Id=.$id; // die(print $sql); $sthr = $dbh-query($sql); // die(var_dump($sthr)); $res = $sthr-fetch(PDO::FETCH_ASSOC); return $res['Name']; } try { $dbh = new PDO('mysql:host=localhost;port=3306;dbname=ins', 'root', '', array(PDO::ATTR_PERSISTENT = false)); $sql = SELECT * FROM Contracts; $sth = $dbh-query($sql); print pre; while($res = $sth-fetch(PDO::FETCH_ASSOC)) { $name = getClientFullName($dbh,$res['ClientId']); print $name.br; } } catch (Exception $e) { print $e-getMessage(); } ? but in my case I can't make it work on my own server. I have removed both PHP and apache and compiled everything for source just to make sure that I will avoid possible problems using rpm. It seems that if I dump $sthr it returns false. Although if I print the query and use it directly it works fine. Apache 2.2.10 PHP 5.2.6 Linux EL5 I've posted this some days ago but none had any idea about this. Do think this could be a bug? obvious but: do a phpinfo() and check PDO is installed on the failing server check db is on the failing server check mysql is running on the failing server check username and pass are correct for db check username and pass combo can access via localhost Yes all that are true because I am able to run a i simple query and retrieve the data normally. Not to mention that I double checked this. But when passing the database handler into a function then I get the results I mentioned before: Call to a member function fetch() on a non-object Nonetheless it is a good tip since someone can fail look in to the obvious. -- Thodoris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Newbie ' If Statement' Question
heredoc is probably the best way to go. There's no way you can mess up your quotes, and you don't have to worry about escaping. Altough I wonder what would happen if you put ? in a heredoc, would it stop processing the php, thinking that it was the end of the php file, or would it just treat it as a string still? - Dan Al [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Here's how I'd do it. ?php $row= $emp_row-getRecordId();//Or, you can put this inside the heredoc with {} //heredoc $str= txt form name=edit method=post action=emp_edit_result2.php input type=hidden name=Status value=Active / input type=hidden name=The_Date value= / input type=hidden name=-recid value=$row / input type=submit name=edit_submit value=Activate / /form txt; if (!empty($emp_row-getField('testfield')) print $str; else print Non Print; ? [EMAIL PROTECTED] wrote: Hello Folks, I would like to be able to wrap a 'form' inside a php 'if statement' - so that the form will appear if the 'if condition' is met. - most likely I cannot have a ?php tag inside another one - and am sure I'm doing other things wrong also... - now I get the error - Parse error: syntax error, unexpected T_STRING in... Q: What is the best way to accomplish this goal? --- ?php if ($emp_row-getField('testfield') !=) { print form name=edit method=post action=emp_edit_result2.php input type=hidden name=Status value=Active/ input type=hidden name=The_Date value=/ input type=hidden name=-recid value=?php echo $emp_row-getRecordId(); ? input type=submit name=edit_submit value=Activate /form ;}else {print Non Print;} ? -- Thanks - RevDave Cool7 @ hosting4days . com [db-lists] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Newbie ' If Statement' Question
Here's how I'd do it. ?php $row= $emp_row-getRecordId();//Or, you can put this inside the heredoc with {} //heredoc $str= txt form name=edit method=post action=emp_edit_result2.php input type=hidden name=Status value=Active / input type=hidden name=The_Date value= / input type=hidden name=-recid value=$row / input type=submit name=edit_submit value=Activate / /form txt; if (!empty($emp_row-getField('testfield')) print $str; else print Non Print; ? [EMAIL PROTECTED] wrote: Hello Folks, I would like to be able to wrap a 'form' inside a php 'if statement' - so that the form will appear if the 'if condition' is met. - most likely I cannot have a ?php tag inside another one - and am sure I'm doing other things wrong also... - now I get the error - Parse error: syntax error, unexpected T_STRING in... Q: What is the best way to accomplish this goal? --- ?php if ($emp_row-getField('testfield') !=) { print form name=edit method=post action=emp_edit_result2.php input type=hidden name=Status value=Active/ input type=hidden name=The_Date value=/ input type=hidden name=-recid value=?php echo $emp_row-getRecordId(); ? input type=submit name=edit_submit value=Activate /form ;}else {print Non Print;} ? -- Thanks - RevDave Cool7 @ hosting4days . com [db-lists] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: SQL Query Statement for MySQL... (DataType -- TEXT vs BLOB)
Scott, As the quote suggests. If you require the ability to query those fields use TEXT, if you do not, use BLOB. Scott Fletcher wrote: I'm wrestling over deciding on which data type to go with, TEXT or BLOB. I have one table with one column of 400 characters, I was thinking that TEXT may be the way to go for that one. I also have another table that use 4 columns of 800 characters along with 5 columns that use 250 characters. I'm thinking of using TEXT for 9 of those columns. The reason is because I read the MySQL Manual there that say TEXT and BLOB are pretty much the same in many ways, the only thing different is that BLOB use VARCHAR Binary while TEXT use VARCHAR. But reading the article somewhere (not part of MySQL's Manual) say this... --snip-- If it doesn't have to be searchable then a BLOB might be more efficient and you shouldn't have to worry about size (Like size is important? ). The reason being that BLOB information is stored seperate from the table data and is related by a reference number in the table. This keeps the table smaller and faster as I understand. --snip-- So, I don't feel too sure what to decide on... Care for some advice or recommendation?? Thanks, Scott Fletcher -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] re: an if statement
[EMAIL PROTECTED] (Andy B) wrote in news:[EMAIL PROTECTED]: although that'll generally work, why not use if(empty($name) || empty($comments)) { dosomething(); } -- ---John Holmes... because for some odd reason on any of the versions of php i am writing for (4.0.6-4.1.3) it always fails Are $name and $comment being passed from a form? In which case you may need to use $_GET or $_POST -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] re: an if statement
although that'll generally work, why not use if(empty($name) || empty($comments)) { dosomething(); } -- ---John Holmes... because for some odd reason on any of the versions of php i am writing for (4.0.6-4.1.3) it always fails -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: an if statement
Hello, Andy! You wrote on Mon, 22 Mar 2004 22:59:02 -0500: AB was just wondering if the statement: AB if(!$name || !$comments){ AB /*whatever here*/ } AB would be interpreted as : if either $name or $comments doesn't exist AB then AB or how would the if statement be?? i need to check if the $name and AB $comments are empty/null because both $name and $comments are AB required to continue in the site (to make a post).. Maybe?.. if (!isset($name) || !isset($comments)) { /*whatever here*/ } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: php path statement appears on my webpages
If you don't run PHP as an CGI but as an module, you don't even need this line. My guess is that you run PHP as a module, so delete the line to fix your problem. HTH, -- Ivo Fokkema PHP MySQL programmer Leiden University Medical Centre Netherlands Jim M Gronquist [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] I include #!/usr/local/bin/php4 at the top of my Php files so that it knows where to find php. Unfortunately the path appears in my web pages. Is there a way for me to turn this off? Is it a setting in Apache or is it something that I change in My php files. #!/usr/local/bin/php4 - Jim Gronquist -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: php path statement appears on my webpages
Ivo, Thanks, but if I leave off this line then I get an error when I try and view my pages. Jim -Original Message- From: Ivo Fokkema [mailto:[EMAIL PROTECTED] Sent: Thursday, July 31, 2003 9:51 AM To: [EMAIL PROTECTED] Subject: [PHP] Re: php path statement appears on my webpages If you don't run PHP as an CGI but as an module, you don't even need this line. My guess is that you run PHP as a module, so delete the line to fix your problem. HTH, -- Ivo Fokkema PHP MySQL programmer Leiden University Medical Centre Netherlands Jim M Gronquist [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] u... I include #!/usr/local/bin/php4 at the top of my Php files so that it knows where to find php. Unfortunately the path appears in my web pages. Is there a way for me to turn this off? Is it a setting in Apache or is it something that I change in My php files. #!/usr/local/bin/php4 - Jim Gronquist -- 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] Re: php path statement appears on my webpages
Thanks, but if I leave off this line then I get an error when I try and view my pages. What kind of error, exactly? You need to determine if you're running PHP as a module or CGI. ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: php path statement appears on my webpages
Have you set Apache httpd.conf? Gronquist, Jim M wrote: Ivo, Thanks, but if I leave off this line then I get an error when I try and view my pages. Jim -Original Message- From: Ivo Fokkema [mailto:[EMAIL PROTECTED] Sent: Thursday, July 31, 2003 9:51 AM To: [EMAIL PROTECTED] Subject: [PHP] Re: php path statement appears on my webpages If you don't run PHP as an CGI but as an module, you don't even need this line. My guess is that you run PHP as a module, so delete the line to fix your problem. HTH, -- Ivo Fokkema PHP MySQL programmer Leiden University Medical Centre Netherlands Jim M Gronquist [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] u... I include #!/usr/local/bin/php4 at the top of my Php files so that it knows where to find php. Unfortunately the path appears in my web pages. Is there a way for me to turn this off? Is it a setting in Apache or is it something that I change in My php files. #!/usr/local/bin/php4 - Jim Gronquist -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: and or statement
What is the and or statement in php? I need this to see if the first statement is whatever, and or the second statement is whatever... if (!isset($PHP_AUTH_USER) and or blah($blah)) {} and and or mean pretty much what they would in English: The expression x AND y only returns true if both x and y are true The expression x OR y returns true if either one of x or y are true One nifty thing, though, is that if PHP *knows* the whole statement will come out true (or false) it won't bother with the rest of it. This is known as a short-circuit. For example, consider this: $x = TRUE; if ($x or really_slow_complicated_function()){ } As soon as PHP figures out the $x part (very quickly) it won't even *BOTHER* to call that really complicated function because it doesn't *need* to call it in order to complete the expression. PHP *KNOWS* that it's all gonna come out true anyway. So the program just skips all the complicated crap in really_slow_complicated_function() and goes on with life. And that's good. Well, it's good more often than it's bad... Sometimes you end up wanting that function called no matter what. No problem: $x = TRUE; $y = really_slow_complicated_function(); if ($x or $y){ } The most common example of the or situation is: $result = mysql_query($query) or die(mysql_error()); In this case, if mysql_query() works then PHP knows not to dink around with the die() stuff because there's no point -- The mysql_query() part will guarantee the whole thing to come out true anyway. *IF* the mysql_query() part works, of course -- If it breaks, you get your death and the error message. It's not a Good Idea to leave die(mysql_error()) in your code on a real server, though. It's too easy for a hacker to break your SQL, force the error message to come out, and that error message could reveal *WAY* too much about how your site it built and give them insider knowledge of the layout of your site/server to break in. A *much* better usage would be: $result = mysql_query($query) or error_log(mysql_error()); Your error messages will be in your Apache error logs (assuming a default php.ini) and while that's a bit more work to dig out, it's also not likely to expose your inner workings to hackers. One of the things I hate the most about VBScripts is boolean expressions don't short-circuit, so you end up having to nest if/else statements like crazy to write decent error-checking code. Ugh! The same sort of thing happens with and when PHP *knows* it's going to turn out FALSE anyway: $x = FALSE; if ($x and really_complicated_function()){ } Here, PHP can just skip the complicated crap, because $x if FALSE, so the $x and ??? *must* be FALSE. PHP also has as well as || which are *exactly* like and and or *EXCEPT* for the order of precedence. If precedence isn't real clear, though, here's a rule of thumb that will probably serve you well: Inside of if() while() for() and such-like, stick with and || Only use or for the or die(...) clause. In the unlikely event that you ever really *NEED* and or or in an if() while() for() part, you'll figure it out when the need arises. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: newbie: mysql statement
I want to update the most recent record (based on the timestamp in field posted) where the parent field == a specified value (in a table called header). I tried the following mysql statement: UPDATE header WHERE parent = '$this-postid' ORDER by posted SET parent='$this-parent' LIMIT1; but apparently you can't use ORDER in an UPDATE statement. If I take order out, the statement works. That being true (and please correct me if its not) how can I ensure that the newest record is the one being acted upon? Perhaps: where posted = max(posted) assuming no *TWO* are ever posted within the same second, which is a very dangerous assumption for a busy site... Can't you just pass in the ID of the actual header you want changed, instead of only its parent? -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: multiple select statement
Natasha [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... hi, i want to know how i can get all selected values of a multiple select statement using $HTTP_POST_VARS The name of your select must be an array, ie select name='myvar[]' multiple optionA optionB optionC /select If A and B are selected, you will get $myvar[0] = 'A' and $myvar[1] = 'B' (for example). The safe way to enumerate the values is with foreach, ie foreach($myvar as $val) { // do something with $val } Hope this helps. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]