Summary: in this tutorial, you will learn how to use the PHP isset() construct to check if a variable is set and not null.
Introduction to the PHP isset() construct #
PHP isset() returns true if a variable is set and not null.
isset(mixed $var): boolThe isset() is a language construct, not a function. Therefore, you cannot assign it to a variable, return it from a function, or call it dynamically via a variable function.
The following example will result in an error:
<?php
$f = isset;Error:
PHP Parse error: syntax error, unexpected ';', expecting '('To work around it, you can create a function that uses the isset() construct and call that function using the variable functions. For example:
<?php
function isset_and_not_null($var): bool
{
return isset($var);
}Or it’s shorter if you use the arrow function syntax:
$isset = fn($var) => isset($var);
var_dump($isset($count)) // falseNote that you’ll learn about the arrow function syntax in the later tutorial.
The following returns false because the $count variable has not been declared:
<?php
var_dump(isset($count));Output:
bool(false)The following example returns true because the $count variable is declared, and its value is different than null:
<?php
$count = 0;
var_dump(isset($count));Output:
bool(true)If you assign null to a variable and pass it to the isset(), the isset() will return false:
<?php
$count = null;
var_dump(isset($count));Output:
bool(false)If you unset a variable, the variable becomes unset. Therefore, the isset() will return false:
<?php
$count = 0;
unset($count);
var_dump(isset($count));Output:
bool(false)Using PHP isset with an array #
If you pass an array element to isset(), it’ll return true. For example:
<?php
$colors = ['primary' => 'blue'];
var_dump(isset($colors['primary']));Output:
bool(true)However, if you pass a non-existing element to isset(), it’ll return false. For example:
<?php
$colors = ['primary' => 'blue'];
var_dump(isset($colors['secondary']));Output:
bool(true)If you pass an array element with value null to isset(), The isset() will return false:
<?php
$colors = ['primary' => 'blue','secondary' => null];
var_dump(isset($colors['secondary']));Output:
bool(false)PHP isset() with string offsets #
The isset() works with string offsets. For example:
<?php
$message = 'Hello';
var_dump(isset($message[0]));Output:
bool(true)If you pass a string element with an invalid offset, the isset() will return false. For example:
<?php
$message = 'Hello';
var_dump(isset($message[strlen($message)]));Output:
bool(false)PHP isset() with multiple variables #
The isset() accepts multiple variables and returns true if all variables are set. The isset() It evaluates the variables from left to right and stops when encountering an unset variable.
isset(mixed $v1, mixed $v2, ...): boolThe following example returns true because all variables $x, $y and $z are set:
<?php
$x = 10;
$y = 20;
$z = 30;
var_dump(isset($x, $y, $z));Output:
bool(true)However, the following example returns false because the variable $y is null, which is not set. Also, the isset() won’t evaluate the variable $z:
<?php
$x = 10;
$y = null;
$z = 30;
var_dump(isset($x, $y, $z));Output:
bool(false)Summary #
isset()is a language construct, not a function.isset()returnstrueif a variable is set and not null.isset()returnstrueif an array element exists and is not null.isset()returnstrueif a string index is valid or false otherwise.isset()returnstrueif all variables are set and not null. It’ll stop evaluating once it encounters an unset variable.
Thank you for your feedback!