Timothy Coetzee Timothy Coetzee - 2 months ago 13
PHP Question

PHP - check if variable is undefined

Consider this jquery statment

isTouch = document.createTouch !== undefined


I would like to know if we have a similar statement in PHP, not being isset() but literally checking for an undefined value, something like:

$isTouch != ""


Is there something similar as the above in PHP?

Answer

An another way is simply :

if($test){
    echo "Yes 1";
}
if(!is_null($test)){
    echo "Yes 2";
}

$test = "hello";

if($test){
    echo "Yes 3";
}

Will return :

"Yes 3"

The best way is to use isset(), otherway you can have an error like "undefined $test".

You can do it like this :

if( isset($test) && ($test!=null) )

You'll not have any error because the first condition isn't accepted.

Comments