3 ms·
`isset` will return true/false and it will only return false if the value is null (never initialized or specifically set to null). Example: $myVar = null;
by joshstrange 2y ago
`isset` will return true/false and it will only return false if the value is null (never initialized or specifically set to null).
Example:
$myVar = null;
var_dump(isset($myVar)); // false
$myVar = '';
var_dump(isset($myVar)); // true
$myVar = '0';
var_dump(isset($myVar)); // true
$myVar = 0;
var_dump(isset($myVar)); // true
If you have a property on a class that was never given a value it will also return `false`:
class MyClass {
public $myProp;
function myFunction() {
var_dump(isset($this->myProp)); // false
}
}
- croes 2y agoSo isset and empty don't actually check what their name suggests. I think the problem is finding a short name for a little complex functionality
- joshstrange 2y agoIf you want to find sane function naming, PHP is only going to disappoint you. I say that as someone who likes PHP, the old function names (and argument order) are… not great. isset is like is_null but one thing isset can do is look at a whole chain (class props or associate array keys) and not blow up. What I mean by that is you can do $test = null; isset($test->fake[‘doesNotExist’]->stilFake); // false Whereas is_null would throw an error trying to walk a fake path. “isset” does essentially a try/catch-type thing around the check it’s doing.