A Look At PHP’s isset()

It’s a language construct, NOT a function

It can not be used as a callable

is_callable('strtoupper');
// true
array_map('strtoupper', ['a', 'b', null, 'd']);
// ['A', 'B', '', 'D']
is_callable('isset');
// false
array_map('isset', ['a', 'b', null, 'd']);
// PHP Warning: array_map() expects parameter 1 to be a valid callback, function 'isset' not found or invalid function name…

It does not accept an expression

if (isset('Hello world')) {
// Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
}
if (isset($a->b())) {
// Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
}
if (isset(! $a)) {
// Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
}
if (isset(CONSTANT)) {
// Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
}

It also checks if a value is null

$value = null;if (isset($value)) {
// ...
}
$value = null;if (array_key_exists('value', get_defined_vars())) {
// ...
}

It can accept multiple arguments

if (isset($a) && isset($b) && isset($c)) {
// ...
}
if (isset($a, $b, $c)) {
// ...
}

It does not trigger “undefined variable/index/property” notices

if (isset($response, $response->list, $response->list['results'], $response->list['results'][0])) {
// ...
}
if (isset($arr[$key], $otherArr[$arr[$key]], $otherArr[$arr[$key]][$otherKey])) {
// ...
}
if (isset($response->list['results'][0])) {
// ...
}
if (isset($otherArr[$arr[$key]][$otherKey])) {
// ...
}

“Undefined method” errors do get triggered

$a = new stdClass();if (isset($a->b()->c)) {
// Fatal error: Uncaught Error: Call to undefined method A::b()…
}
if (isset($a->b->c()->d)) {
// Fatal error: Uncaught Error: Call to a member function c() on null…
}
if (isset($a) && method_exists($a, 'b') && isset($a->b()->c)) {
// ...
}
if (@$a->b()->c !== null) {
// ...
}

!empty() is not quite the same

$a = [];if (empty($a['b']->c)) {
// ...
}
$a = '0';if (isset($a)) {
// It IS set
}
if (empty($a)) {
// It IS empty
}

Null coalesce operator

$result = isset($value) ? $value : 'fallback';
$result = $value ?? 'fallback';
$value ??= 'fallback';

It does not evaluate the __get() magic method

class Person
{
protected $attributes = [];
public function __get($name)
{
return $this->attributes[$name] ?? null;
}
public function __set($name, $value)
{
$this->attributes[$name] = $value;
}
}
$person = new Person();
$person->name = 'Liam';
echo $person->name; // 'Liam'isset($person->name); // false
class Person
{
// ...
public function __isset($name)
{
return isset($this->attributes[$name]);
}
}
$person = new Person();
$person->name = 'Liam';
isset($person->name); // true
isset($person->somethingElse); // false

You can pass non-existent variables to userland functions

interface NullObject {}class Logger {
// ...
}
class NullLogger extends Logger implements NullObject {
// ...
}
function is_truthy(&$value)
{
if ($value instanceof NullObject) {
return false;
}
return (bool) $value;
}
is_truthy($a);
// false
$b = '';
is_truthy($b);
// false
$c = '1';
is_truthy($c);
// true
$logger = new Logger();
is_truthy($logger);
// true
$nullLogger = new NullLogger();
is_truthy($nullLogger);
// false
$a = [];is_truthy($a['b']['c']);
// false
var_dump($a);
// [
// 'b' => [
// 'c' => null,
// ],
// ]

Conclusion

--

--

--

PHP developer based in the South-West of England.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Enroute Universal Gateway

Digital Publishing Just (Finally) Got An Upgrade

A Git crash course for you — Commit changes

10 Best Courses to Learn Quarkus, DropWizard, Vert.x, and MicroNaut in 2022

10 Best Courses to Learn Quarkus, DropWizard, Vert.x, and MicroNaut

Building a docker image & storing it in the Gitlab Container Registry

Creating Python Virtual Environment and Managing Dependency

CloudFormation vs TerraForm for Managing Your Site’s DNS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Liam Hammett

Liam Hammett

PHP developer based in the South-West of England.

More from Medium

How To Enable PHP Debugging with XDebug and Visual Studio Code (Windows and Xampp)

How I set up a new Mac in minutes with a simple Homebrew script

How to Hire the Best PHP Developers?

How to xdebug when your PHPStorm runs with xserver inside WSL2, but you use Docker Desktop on…