PHP Variable Testing

Important notes on the comparisons and tests:

There appear to be two null values at the start of the test. The first one is actually an unset variable, i.e. no longer in existence. The second is a variable which was set to null.

Tests with Ctype and strcmp-type functions use the system default C locale. Results may vary for other locales.

Some tests might seem a bit ‘silly’, for instance testing with === 'null'. The reason for adding these tests can be divided into two categories:

Data received from databases and $_POST/$_GET/etc variables are always received as strings (unless a potentially used database abstraction layer changes this). So sometimes testing for a string value where a non-string variable type would be more logical, can actually make sense.

Some are unfortunately regularly encountered in code and added here to illustrate why not to use them.

BEWARE: some variable changing functions will not return a changed value, but will return whether the changing succeeded. You will find this indicated in the function header by the use of $copy (as the test will use a copy of the variable to not influence the other tests in the same table).

Notes on some variables:

Some of the test variables used, do not print the way they are set, either because they contain invisible characters or because they result in something else, so for your convenience, these are outlined here:

†

How the variable is defined:

i8

$x = 0xCC00F9; // Hexadecimal integer.

i9

$x = 052; // Octal integer.

ia

$x = 0b0111001; // Binary integer (PHP5.4+).

ib

$x = ‏௫‏; // Tamil digit five - entered as string as PHP itself cannot deal with it as an integer.

$x = ⅕; // Unicode character representing 1/5 - entered as string as PHP itself cannot deal with it as a float.

sk

$x = "123, \"str\"\r\n";

sn

$x = "\f\t\r\n";

sp

$x = "\x7f\t\r\n"

Legend / How to use the tables:

The error level for this test sheet has been set to E_ALL & ~E_STRICT. All errors are caught and referenced (with #links) in the tables with details of the error messages (if any) below each table. Similar error messages are grouped together for your convenience.

Some column labels have been shortened to avoid them taking undue space. These are indicated by a …. If you mouse-over the column label you will see the full variable/test information.

In comparison tables, the left-top table header will indicate the comparison used and link to the relevant page in the PHP Manual.

In test tables, the left top table header indicates the type of tests. Both this header as well as most column headers link to their respective relevant PHP Manual pages.

A ‡ with a number next to a column header means there is a (linked) footnote for that entry at the bottom of the page.

When you mouse-over the table the row and column you are at are highlighted. To help you compare specific columns/rows, you can click on any cell to mark the column and row which the cell intersects for extra highlighting. Click again to remove this sticky highlight.

Legend to the color coding

NULL:

null

Boolean:

true
/ false

Integer:

123456789
/ 0

Float:

12345.6789

String:

‘A string’

Array and Object:

Indicated as such. Array keys, array values and object properties will be color coded according to the coding shown here.

Notice: Object of class stdClass/TestObject/TestObjectToString could not be converted to int

‡ 1

Please note: On some PHP versions filter_var( $x, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ) where $x = false will incorrectly return null.
Also: with the same parameters filter_var() will return false instead of null for most objects.

‡ 2

The code snippet is simplified for brevity. Please refer to the source of this file on GitHub for full details on how to use filter_var_array().

Important: Integers between -128 and 255 are interpreted as the ASCII value pointing to a character (negative values have 256 added in order to allow characters in the Extended ASCII range).
In any other case, integers are interpreted as a string containing the decimal digits of the integer.

Important: Integers between -128 and 255 are interpreted as the ASCII value pointing to a character (negative values have 256 added in order to allow characters in the Extended ASCII range).
In any other case, integers are interpreted as a string containing the decimal digits of the integer.

Warning: is_nan() expects parameter 1 to be float, string/array/object/resource given

Warning: is_finite() expects parameter 1 to be float, string/array/object/resource given

Warning: is_infinite() expects parameter 1 to be float, string/array/object/resource given

Notice: A non well formed numeric value encountered

Warning: preg_match() expects parameter 2 to be string, array/object/resource given

Notice: Object of class stdClass/TestObject/TestObjectToString could not be converted to int

‡ 1

Important: Integers between -128 and 255 are interpreted as the ASCII value pointing to a character (negative values have 256 added in order to allow characters in the Extended ASCII range).
In any other case, integers are interpreted as a string containing the decimal digits of the integer.

(Catchable) Fatal error: Object of class stdClass/TestObject/TestObjectToString could not be converted to string

‡ 1

Depending on your error handling settings/exception catching and your PHP version, using settype( $copy, 'string' ) on an object which does not have a __toString() method will result either in the string Object or will return a (catchable) fatal error.

‡ 2

Before PHP < 5.2.0, the __toString() method was only available to echo/print statements, so casting to string would still result in Object, even when the object contained a __toString() method.

‡ 3

The code snippet is simplified for brevity. Please refer to the source of this file on GitHub for full details on how to use filter_var_array().

Warning: preg_match() expects parameter 2 to be string, array/object/resource given

Warning: strlen() expects parameter 1 to be string, array/object/resource given

Warning: count_chars() expects parameter 1 to be string, array/object/resource given

Warning: array_sum() expects parameter 1 to be array, null given

Warning: mb_strlen() expects parameter 1 to be string, array/object/resource given

Notice: Undefined offset: 2

Warning: trim() expects parameter 1 to be string, array/object/resource given

Fatal error: Cannot use object of type stdClass/TestObject/TestObjectToString as array

‡ 1

Important: Integers between -128 and 255 are interpreted as the ASCII value pointing to a character (negative values have 256 added in order to allow characters in the Extended ASCII range).
In any other case, integers are interpreted as a string containing the decimal digits of the integer.

‡ 2

What \w matches is locale dependent. The locale for these cheatsheets are set to C. Results with other locales will vary.

Warning: count(): Parameter must be an array or an object that implements Countable

Warning: array_key_exists() expects parameter 2 to be array, null/boolean/integer/double/string/object/resource given

Warning: key() expects parameter 1 to be array, null/boolean/integer/double/string/object/resource given

Warning: current() expects parameter 1 to be array, null/boolean/integer/double/string/object/resource given

Warning: array_filter() expects parameter 1 to be array, null/boolean/integer/double/string/object/resource given

Warning: Illegal string offset 'foo'

Notice: Uninitialized string offset: 0

Warning: Illegal string offset 'bar'

Notice: Undefined index: foo

Fatal error: Cannot use object of type stdClass/TestObject/TestObjectToString as array

‡ 1

Whether to use isset() or array_key_exists() depends on what you want to know:

isset( $array[$key] ) will tell you whether the array key exists and has been assigned a value.
Used on its own it will give undesired results when used on strings as characters within a string can be approached using the $x[] syntax as well.
Also, it will cause fatal errors when used on objects.

array_key_exists( $key, $array ) will tell you only whether the array key exists, but will not check whether it has a value assigned to it.
Used on its own, it will show a warning if $array is not an array

This said, we can concluded that to avoid warnings and undesired results you will always have to use an is_array() first.
Also note that isset() is faster than array_key_exists(), but as said above, will return false if no value has been assigned.