Pixelastic

You can cut our wings but we will always remember what it was like to fly.

Posts tagged with "array"

Another PHP casting weirdness

Second weird PHP behavior behavior of the day :

$foo = false;
echo $foo['bar']; // Ouptuts nothing, but doesn't throw an error either

Strange. I try to read an undefined index (false is not an array), but PHP doesn't complain. That's weird.

$foo = true;
echo $foo['bar']; // Throws an error

This time, PHP tells me that the index is not defined and throws an error. Wait, what ?

Apparently, this is the intended behavior, but it does seem a bit strange...

Deleting an element from an Array/Object in Javascript

I'm going to post that here because it's the second time I stumble upon this "problem" and the second time I lost some precious time to understand what was going on.

I had a Javascript Array, named elements and I wanted to remove one of its properties by its index i.

I know that merely calling elements[i] = null won't work (the property will still be present in the array and the length won't be updated.

Calling delete elements[i] won't work either. Same result.

I had to use elements.splice(i, 1) to effectively remove the element and update the length value.

Also note that sometimes I accidentally declare an Array when what I really want is an Object. I tend to forgot that associative Arrays in Javascript do not really exists, they just are Objects.