PHP isset

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): boolCode language: PHP (php)

The 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;Code language: PHP (php)

Error:

PHP Parse error:  syntax error, unexpected ';', expecting '('Code language: PHP (php)

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);
}Code language: HTML, XML (xml)

Or it’s shorter if you use the arrow function syntax:

$isset = fn($var) => isset($var);

var_dump($isset($count)) // falseCode language: PHP (php)

Note 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));Code language: PHP (php)

Output:

bool(false)Code language: PHP (php)

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));Code language: PHP (php)

Output:

bool(true)Code language: PHP (php)

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));Code language: PHP (php)

Output:

bool(false)Code language: PHP (php)

If you unset a variable, the variable becomes unset. Therefore, the isset() will return false:

<?php

$count = 0;
unset($count);
var_dump(isset($count));Code language: PHP (php)

Output:

bool(false)Code language: PHP (php)

Using PHP isset with array

If you pass an array element to isset(), it’ll return true. For example:

<?php

$colors = ['primary' => 'blue'];

var_dump(isset($colors['primary']));Code language: PHP (php)

Output:

bool(true)Code language: PHP (php)

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']));Code language: PHP (php)

Output:

bool(true)Code language: PHP (php)

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']));Code language: PHP (php)

Output:

bool(false)Code language: PHP (php)

PHP isset() with string offsets

The isset() works with string offsets. For example:

<?php

$message = 'Hello';
var_dump(isset($message[0]));Code language: PHP (php)

Output:

bool(true)Code language: PHP (php)

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)]));Code language: PHP (php)

Output:

bool(false)Code language: PHP (php)

PHP isset() with multiple variables

The isset() accepts multiple variables and returns true if all variables are set. The isset() evaluates the variables from left to right and stops when it encounters an unset variable.

isset(mixed $v1, mixed $v2, ...): boolCode language: PHP (php)

The 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));Code language: PHP (php)

Output:

bool(true)Code language: PHP (php)

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));Code language: PHP (php)

Output:

bool(false)Code language: PHP (php)

Summary

  • isset() is a language construct, not a function.
  • isset() returns true if a variable is set and not null.
  • isset() returns true if an array element exists and not null.
  • isset() returns true if a string index valid or false otherwise.
  • isset() returns true if all variable are set and not null. It’ll stop evaluating once it encounter an unset variable.
Did you find this tutorial useful?