From: steve_r on 13 Apr 2010 12:04 I'm new to programming, drive a truck in the day, now taking night courses to get a better job for my family. Please bear with me if this is a dumb question, I don't have much experience. I'm taking a night class in HTML and PHP and can't figure out a problem and can't find the answer in the book for the course ("Beginning PHP5" by Wrox Press), on the switch manual page on php.net, or in any postings to this mailing list. I'm trying to pass a value to a simple integer to a function, and then use that value in a switch statement. The problem I'm having is that regardless of the value of 'val', the first case statement always executes. Even if I put '$val = 0' right before the case statement, the first case statement executes. The syntax looks correct based on the php.net man page for switch and from the user examples. It also matches the example in the book. function check_it2($val) { echo gettype($val); switch($val) { case($val > 0 ): echo "Switch greater than 0"; $diff_obj = 1; break; case($val < 0 ): echo "Less than 0"; $diff_obj = -1; break; default: echo "Equal to 0"; $diff_obj = 0; } print("Here's \$diff_obj2 in the function: " . $diff_obj); return $diff_obj; } I even put the following code before the switch statement just to make sure I'm not crazy: $val = 0; if($val > 0) { echo "If greater than 0"; } else { echo "If not greater than 0"; } and it falls through to the else as it should. I've tried putting single and double quotes around the case variables but it always prints out the first value. I've recoded to use a series of if statements but why isn't the switch working? I've read through the 'loose comparison' section, but nothing appears to apply there. Sorry for the basic question. Steve Reilly
From: Ashley Sheridan on 13 Apr 2010 12:06 On Tue, 2010-04-13 at 12:04 -0400, steve_r wrote: > I'm new to programming, drive a truck in the day, now taking night courses > to get a better job for my family. Please bear with me if this is a dumb > question, I don't have much experience. > > I'm taking a night class in HTML and PHP and can't figure out a problem and > can't find the answer in the book for the course ("Beginning PHP5" by Wrox > Press), on the switch manual page on php.net, or in any postings to this > mailing list. > > I'm trying to pass a value to a simple integer to a function, and then use > that value in a switch statement. The problem I'm having is that regardless > of the value of 'val', the first case statement always executes. Even if I > put '$val = 0' right before the case statement, the first case statement > executes. The syntax looks correct based on the php.net man page for switch > and from the user examples. It also matches the example in the book. > > function check_it2($val) { > echo gettype($val); > switch($val) { > case($val > 0 ): > echo "Switch greater than 0"; > $diff_obj = 1; > break; > case($val < 0 ): > echo "Less than 0"; > $diff_obj = -1; > break; > default: > echo "Equal to 0"; > $diff_obj = 0; > } > print("Here's \$diff_obj2 in the function: " . $diff_obj); > return $diff_obj; > } > > I even put the following code before the switch statement just to make sure > I'm not crazy: > > $val = 0; > if($val > 0) { > echo "If greater than 0"; > } > else { > echo "If not greater than 0"; > } > > and it falls through to the else as it should. > > I've tried putting single and double quotes around the case variables but it > always prints out the first value. I've recoded to use a series of if > statements but why isn't the switch working? I've read through the 'loose > comparison' section, but nothing appears to apply there. > > Sorry for the basic question. > > Steve Reilly Change the first line of the switch to switch(true) and it will be functioning as you want. Normally, a switch has this form: switch($val) { case 1: { // statements break; } case 10: { // statements break; } default: { // statements } } But PHP does allow you to use variable cases (as you have in your example) if the value in the switch is a boolean (true or false). It can be a little confusing if you're new to PHP (or programming in general) but you'll get used to it after using it a few times. Thanks, Ash http://www.ashleysheridan.co.uk
From: Robert Cummings on 13 Apr 2010 12:12 steve_r wrote: > I'm new to programming, drive a truck in the day, now taking night courses > to get a better job for my family. Please bear with me if this is a dumb > question, I don't have much experience. > > I'm taking a night class in HTML and PHP and can't figure out a problem and > can't find the answer in the book for the course ("Beginning PHP5" by Wrox > Press), on the switch manual page on php.net, or in any postings to this > mailing list. > > I'm trying to pass a value to a simple integer to a function, and then use > that value in a switch statement. The problem I'm having is that regardless > of the value of 'val', the first case statement always executes. Even if I > put '$val = 0' right before the case statement, the first case statement > executes. The syntax looks correct based on the php.net man page for switch > and from the user examples. It also matches the example in the book. > > function check_it2($val) { > echo gettype($val); > switch($val) { > case($val > 0 ): > echo "Switch greater than 0"; > $diff_obj = 1; > break; > case($val < 0 ): > echo "Less than 0"; > $diff_obj = -1; > break; > default: > echo "Equal to 0"; > $diff_obj = 0; > } > print("Here's \$diff_obj2 in the function: " . $diff_obj); > return $diff_obj; > } You're a tad confused :) Q: What is the result of $val > 0? A: false. Q: What is the value of $val? A: 0 Q: Is 0 equivalent to false? A: Yes! Use an if statement for this kind of logic. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP
From: steve_r on 13 Apr 2010 12:15 Okay, I understand now, true/false is better, now it makes sense why it's always hitting the case it hits. Thank you Ashley.
From: tedd on 13 Apr 2010 13:19 At 12:04 PM -0400 4/13/10, steve_r wrote: >I'm new to programming, drive a truck in the day, now taking night courses >to get a better job for my family. Please bear with me if this is a dumb >question, I don't have much experience. > >-snip- > >Sorry for the basic question. > >Steve Reilly Steve: Please review: http://php1.net/c/switch/ Note: 1. The value of the switch statement in this case is "true" (it could also be '1'). This is different than what's found in most other case/switch control structures in other languages. 2. Generally you should use functions to process and return values and then decide as to what to do with the results, such as print(). Putting print in functions is fine if the function is supposed to print something. 3. Try to make your variables and function names simple and syntactical -- it will help you later when your code becomes more complex. Good luck with trying to make a better living programming than driving a truck. If you want to say thanks for my effort, please answer me this -- with regard to the class you are talking: What is the name of the class? How many credits is it? What's the name of the textbook you are using? And, if there is a syllabus, can I have a copy? Thanks, tedd -- ------- http://sperling.com http://ancientstones.com http://earthstones.com
|
Next
|
Last
Pages: 1 2 3 4 5 6 Prev: Saving form data into session before leaving a page Next: Array differences |