NYCPHP Meetup

NYPHP.org

[nycphp-talk] comparison with zero

Michael Southwell michael.southwell at nyphp.org
Thu Jul 20 23:29:27 EDT 2006


All strings are evaluated as zero unless they begin with a character 
which can be interpreted as a numeral.  You can test this as follows:

echo intval( 'a string');

see 
http://us3.php.net/manual/en/language.types.string.php#language.types.string.conversion 
for more information

this is why echo 5 + '5' outputs 10, whereas echo 5 + 'five' outputs 5


At 10:05 PM 7/20/2006, you wrote:
>Hi everyone,
>
>Okay, here's a test.
>
>Consider the following snippet:
>
><?php
>define('ZERO', 0);
>define('ONE', 1);
>
>function switchIt($val){
>
>         switch($val){
>                 case ZERO:
>                 echo 'it is zero';
>                 break;
>
>                 case ONE:
>                 echo 'it is one';
>                 break;
>
>                 default:
>                 echo 'I do not know';
>                 break;
>         }
>
>}
>
>switchIt('undefined');
>?>
>What will be outputted above?
>
>If you said 'I do not know', then you are wrong; 'it is zero' will be
>outputted.
>
>The reason I think this is happening is because if you do something like
>this:
>
>if('one' == 0){
>         echo 'one is equal to zero';
>}
>you will get 'one is equal to zero'. This may be a stupid question, but
>why if you compare a string to zero, does it evaluate to true?
>
>Thanks,
>
>- Jay
>_______________________________________________
>New York PHP Community Talk Mailing List
>http://lists.nyphp.org/mailman/listinfo/talk
>
>NYPHPCon 2006 Presentations Online
>http://www.nyphpcon.com
>
>Show Your Participation in New York PHP
>http://www.nyphp.org/show_participation.php

Michael Southwell, Vice President for Education
New York PHP
http://www.nyphp.com/training - In-depth PHP Training Courses 




More information about the talk mailing list