Sep 042014
Article PHP

While a PHP script is executing, a given variable can be assigned values of different data types: simple types (integer, boolean, string,…) as well as complex types (array). At some point during its execution, a script might need to check the type of data stored in a variable.

Arrays are a special case. An array may be created as a sequence of values (that may be simple or complex), or else as a set of (key,value) pairs, where the key is a string, and the value is any data of simple or complex data type.

This post explains how to check in PHP the data type of the value assigned to a variable, specially in the case of arrays.

built-in PHP functions to check the data type

The PHP interpreter implements a set of functions that can be used to check the data type of the value assigned to a variable:

  • is_null
  • is_scalar
    • is_bool
    • is_numeric
      • is_int
      • is_integer
      • is_long
      • is_real
        • is_float
        • is_double
    • is_string
  • is_ array

Besides, the value of a variable might be a reference to a function, an object, or a resource. There are also built-in functions to check if the variable contains a value of one of these types:

  • is_ callable
  • is_ object
  • is_ resource

Sequential and associative arrays

An array can be initialized in PHP in two different ways, as an ordered sequence of values, or as a set of (key, value) pairs. There is a built-in function is_array that checks if the value of a variable is of type array. However, there is no built-in function to differentiate these two types of array.

1. Example array initialization as a sequence of values:

2. Example array initialization as a set of (key,value) pairs:

In both examples above, the call to is_array returns “true”, but there is no way to tell between both array types.

(The second example shows that one of the values in an array may in turn be of array type).

The reason is that, in PHP, a sequential array is treated internally as an associative array, that is, as a set of (key, value) pairs, where the keys are the numerical indexes of the array elements (zero based).

Therefore, the two array definitions in the following example are equivalent:

The resulting arrays are identical, and array elements are accessed as $data[INDEX]

In an associative array, the value for a given key is accessed as $data[KEY]


How to check if an array is sequential or associative

There are several possible ways to check the array type. In all cases, they check if all keys in the array constitute a sequence 0,1,…(n-1) of integers, where n is the number of elements in the array

The most straightforward way of coding this check is:

The function range returns an array 0,1,…(n-1) of integer values.

The function array_keys returns a sequential array whose values are the keys in the associative array.

The !== operator performs a comparison without performing any type conversion of the values in both arrays.

For instance the following array is identified by is_assoc as associative:

This is because the keys (“00”, “01”) are considered to be strings, and different from the numerical sequence 0,1

However, if the array is initialized as:

In this case, the is_assoc() function returns false.

If the operator != had been used in the is_assoc() function, both example arrays would have been identified as sequential arrays, because the keys in the first array would have been converted into their integer equivalents.


Index of posts about programming in PHP


 Posted by at 7:47 am

 Leave a Reply