note 89078 modified in function.is-null by danbrown

From: Date: Fri, 20 Feb 2009 16:26:23 +0000
Subject: note 89078 modified in function.is-null by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-150898@lists.php.net to get a copy of this message
If you want to test if a class is null or not, always use is_null($class) instead of "$class == null". If the class instance has no members, then "$class == null" will be true. Here is an example: <?php class ClassA { function printSomething() { print('Something'); } } class ClassB { var $doNothing; function ClassB() { $this->doNothing = 'Nothing'; } function printSomething() { print('Something'); } } $a = new ClassA(); print('<br> #1: ' . ($a == null ? '$a is null' : '$a is not null')); print('<br> #2: ' . (is_null($a) ? '$a is null' : '$a is not null')); $b = new ClassB(); print('<br> #3: ' . ($b == null ? '$b is null' : '$b is not null')); print('<br> #4: ' . (is_null($b) ? '$b is null' : '$b is not null')); ?> The output of above is: #1: $a is null #2: $a is not null #3: $b is not null #4: $b is not null --was-- If you want to test if a class is null or not, always use is_null($class) instead of "$class == null". If the class instance has no members, then "$class == null" will be true. Here is an example: class ClassA { function printSomething() { print('Something'); } } class ClassB { var $doNothing; function ClassB() { $this->doNothing = 'Nothing'; } function printSomething() { print('Something'); } } $a = new ClassA(); print('<br> #1: ' . ($a == null ? '$a is null' : '$a is not null')); print('<br> #2: ' . (is_null($a) ? '$a is null' : '$a is not null')); $b = new ClassB(); print('<br> #3: ' . ($b == null ? '$b is null' : '$b is not null')); print('<br> #4: ' . (is_null($b) ? '$b is null' : '$b is not null')); The output of above is: #1: $a is null #2: $a is not null #3: $b is not null #4: $b is not null http://php.net/manual/en/function.is-null.php

« previous php.notes (#150898) next »