Main Content

If nothing, make it nothing.

Archive - Originally posted on "The Horse's Mouth" - 2009-06-02 12:38:43 - Graham Ellis

Here's a peculiar PHP statement I wrote this morning:
  if ($aindex[$chk] == "") $aindex[$chk] = "";

What was I doing? I am actually checking to see whether there's anything in an array element and if there isn't (which could be because it's empty or because it doesn't yet exist), then it should be created if necessary and made to contain an empty string. In PHP, = = tests whether two expressions have an equal values, but = = = tests whether they are of the same type and with equal value.

The important aspect of the assignment that's made above is that it ensures that the array ends up with an element who's key is the contents of $chk, so that later on in my code I can go though all of the items using array_keys even for $chk values for which there is no data.