PDA

View Full Version : operator !, checking variable for number (0,1,2,3) or nothing


piz
05-18-2003, 11:40 PM
Hi,

I am checking a variable with this code:

if(!$var){...

The value of $var can be nothing, 0, 1, 2, 3, or 4.
The problem is now, that if the value is 0 the expression (!$var) returns true, too.

Is there an easy way to check the difference between 0 and nothing without having to create some of those non-elegant solutions?

Thx.
Saludo,
piz.

piz
05-18-2003, 11:46 PM
Ok... I just found a solution using is_numeric($var).

But i am still interested, is there another way, perhaps a different operator than just the '!' to make difference between 0 and nothing?
Something like '===' but checking only one variable?

I don't like checking for FALSE or NULL because thats different in some PHP versions.

firepages
05-19-2003, 02:00 AM
you can utilise isset() to test if a variable has been set or not.


<?
$yaks=0;
if( isset( $yaks ) ){
echo '$yaks is set';
}else{
echo '$yaks is not set';
}
?>