Re: Basic switch statement

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux