Re: Basic switch statement

From: Date: Tue, 13 Apr 2010 21:17:29 +0000
Subject: Re: Basic switch statement
References: 1 2 3 4 5 6 7  Groups: php.general 
Request: Send a blank email to php-general+get-304072@lists.php.net to get a copy of this message
Nathan Rixham wrote:
Robert Cummings wrote:
Nathan Rixham wrote:
Robert Cummings wrote:
Nathan Rixham wrote:
Robert Cummings wrote:
steve_r wrote:
I'm new to programming function check_it2($val) {
    echo gettype($val);
    switch($val) {
        case($val > 0 ):
            echo "Switch greater than 0";
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.
This is a fantastic example of false logic and an easy pitfall. in fact this would make a great interview question! to expand a little on the various scenarios (just for clarity, Rob is right) $val = 1; 1 > 0 equates to TRUE is 1 equivalent to TRUE : YES $val = 0; 0 > 0 equates to FALSE is 0 equivalent to FALSE : YES $val = -1; -1 > 0 equates to FALSE is -1 equivalent to FALSE: YES so no matter what value you set $val to; it's always true.
Fail on that last one. -1 is not equivalent to FALSE :B
well that's one job I'm not getting :p
Well you DID get 66.7%. I've met "coders" that would stare at the answer and still not understand :D
the travesty is that I spent most of yesterday on trains brushing up on / studying formal logic!
Muphry's Law!!! http://en.wikipedia.org/wiki/Muphry's_law *heheh* Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP

« previous php.general (#304072) next »