Blog Archives

PHP isset() and multi-dimentional array

** The issue discussed in this post has been fixed since PHP5.4.0, so the below discussion and solution are for PHP 5.3.x or lower. Thanks David for clarifying. A few weeks ago I covered how to check the existence of an array element in PHP. In the post I explained why isset() is dangerous to […]

Check if an array is associative or sequential(indexed) in PHP

In PHP, depending on the type of the keys in an array, the array can be identified as associative (if the key is a string like $arr[‘mykey’]) or sequential (some may call it indexed, i.e. if the key is an integer like $arr[1], $arr[2], etc.). Although the internal representation of both associative and sequential arrays […]

PHP – isset() vs array_key_exists() : a better way to determine array element’s existence

The story In the CourseYou project, we’re asked to check if an element is set in an array. That is,  we’re asked to determine whether $Arr[‘MyElement’] exists. So we use the following code as a start. <?php if (isset($Arr[‘MyElement’])) { … do my stuff … } ?> This code works fine, but, it works fine […]

Equal (==), identical (===) and array comparison in PHP

Equal (==) If you use equal (==), you are allowing type conversion which means PHP will try to convert the two sides into the same type and then do the comparison. So even if the two sides are NOT the same thing, they MAY still be treat as the SAME. Consider this code: <?php $left […]