PHP Puzzlers #2

Here’s another short post detailing some odd behavior in PHP.

Let’s have a look at the following simple piece of code:

$arr = array(
  1 => 'Hello',
  1.5 =>'World',
  true=>"!"
);
var_dump($arr);

What would the output of this be? Surprisingly this does the following:

array(1) {
  [1]=>
  string(1 ...
more ...

PHP Puzzlers #1

PHP has some idiosyncrasies that can cause odd or counter intuitive behavior. This will be a series of short posts covering some of these situations.

Let’s have a look at the following simple piece of code:

$arr = array('foo','bar','baz');


foreach($arr as &$item){
   echo "First array: ".$item ...
more ...