Variable Testing in PHP

PHP How To

At times, we need to check whether a variable has been set, especially in cases where there is some user input from a form, and we need to verify it before saving it to the database. PHP provides a number of built-in constructs that can be used for this purpose.

images/articles/php/variable-testing-in-php.jpg

1. isset()

The isset() returns true if the variable exists and has been assigned a value other than null.

isset($a); // false
$a = 10;

isset($a); // true
$a = null;

isset($a); // false

2. empty()

The empty construct checks whether the specified variable has an empty value - such as null, 0, false, or an empty string - and returns true if that is the case. It also returns true if the variable does not exist.

empty($b); // true
$b = false;
empty($b); // true

3. is_null()

The is_null construct can be used to test whether a variable is set to null.

$c = null;
is_null($c); // true

$c = 10;
is_null($c); // false

If the variable does not exist, is_null also returns true, but with an error notice because it is not supposed to be used with uninitialized variables.

4. unset()

It deletes a variable from the current scope.

$e = 10;
unset($e); // delete $e

5. Null Coalescing Operator

The null coalescing operator (??) was added in PHP 7 as a shortcut for the common case of using a ternary with isset. It returns its first operand if it exists and is not null; otherwise, it returns its second operand.

$x = null;
$name = $x ?? 'unknown'; // "unknown"

This statement is equivalent to the following ternary operation, which uses the isset construct.

$name = isset($x) ? $x : 'unknown';

6. Determining Types

PHP has several useful functions for determining the type of a variable.

  • is_array() True if variable is an array.
  • is_bool() True if variable is a bool.
  • is_callable() True if variable can be called as a function.
  • is_float(), is_double(), is_real() True if variable is a float.
  • is_int(), is_integer(), is_long() True if variable is an integer.
  • is_null() True if variable is set to null.
  • is_numeric() True if variable is a number or numeric string.
  • is_scalar() True if variable is an int, float, string, or bool.
  • is_object() True if variable is an object.
  • is_resource() True if variable is a resource.
  • is_string() True if variable is a string.

7. Variable Information

PHP has three built-in functions for retrieving information about variables: print_r, var_dump, and var_export.

The print_r function displays the value of a variable in a human-readable way. It is useful for debugging purposes.

$a = array('one', 'two', 'three');
print_r($a);

The preceding code produces the following output.

Array ( [0] => one [1] => two [2] => three )

Similar to print_r is var_dump, which in addition to values, also displays data types and sizes. Calling var_dump($a) shows this output.

array(3) {
[0]=> string(3) "one"
[1]=> string(3) "two"
[2]=> string(5) "three"
}

The var_export function prints variable information in a style that can be used as PHP code. The following shows the output for var_export($a).

array ( 0 => 'one', 1 => 'two', 2 => 'three', )