Jump to content.

The primary purpose of this page was to determine if undeclared variables can be passed to PHP functions like is_string(). The short answer is no. If there is the potential for an undeclared variable to be passed to functions like is_string(), then you must use the isset() function first.

Example:

if(isset($foo) && is_string($foo)) {
    // Do something with $foo
}

For the sample code used on this page, including the custom error handling, See my blog post: Can is_string() be used without an isset() for unset variables in PHP?


$foo = 123

$foo is set.
$foo isset, but is not a string.
$foo is not a string.
$foo is not a float.
$foo is an integer.
$foo is not a boolean.
$foo is not an object.
$foo is not an array.

$foo = 123.456

$foo is set.
$foo isset, but is not a string.
$foo is not a string.
$foo is a float.
$foo is not an integer.
$foo is not a boolean.
$foo is not an object.
$foo is not an array.

$foo = '123'

$foo is set.
$foo isset and is a string.
$foo is a string.
$foo is not a float.
$foo is not an integer.
$foo is not a boolean.
$foo is not an object.
$foo is not an array.

$foo = 'abc'

$foo is set.
$foo isset and is a string.
$foo is a string.
$foo is not a float.
$foo is not an integer.
$foo is not a boolean.
$foo is not an object.
$foo is not an array.

$foo = date('r')

$foo is set.
$foo isset and is a string.
$foo is a string.
$foo is not a float.
$foo is not an integer.
$foo is not a boolean.
$foo is not an object.
$foo is not an array.

$foo = true

$foo is set.
$foo isset, but is not a string.
$foo is not a string.
$foo is not a float.
$foo is not an integer.
$foo is a boolean.
$foo is not an object.
$foo is not an array.

$foo = false

$foo is set.
$foo isset, but is not a string.
$foo is not a string.
$foo is not a float.
$foo is not an integer.
$foo is a boolean.
$foo is not an object.
$foo is not an array.

$foo = array('1', 'a')

$foo is set.
$foo isset, but is not a string.
$foo is not a string.
$foo is not a float.
$foo is not an integer.
$foo is not a boolean.
$foo is not an object.
$foo is an array.

$foo = jamObject()

$foo is set.
$foo isset, but is not a string.
$foo is not a string.
$foo is not a float.
$foo is not an integer.
$foo is not a boolean.
$foo is an object.
$foo is not an array.

unset($foo)

$foo is not set.
$foo is not set, so is not string.
* Notice: Undefined variable: foo *
$foo is not a string.
* Notice: Undefined variable: foo *
$foo is not a float.
* Notice: Undefined variable: foo *
$foo is not an integer.
* Notice: Undefined variable: foo *
$foo is not a boolean.
* Notice: Undefined variable: foo *
$foo is not an object.
* Notice: Undefined variable: foo *
$foo is not an array.