NYCPHP Meetup

NYPHP.org

[nycphp-talk] comparison with zero

Jayesh Sheth jay_nyphp2 at emailias.com
Thu Jul 20 22:05:11 EDT 2006


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



More information about the talk mailing list