foreach and references

Apparently this is not a bug and it's documented:

<?php
$array
= array('foo', 'bar');
foreach (
$array as &$foo);
foreach (
$array as $foo);
var_dump($array);
?>

»

Array reference always succeeds

php -r 'var_dump($a["foo"]);var_dump(&$a["foo"]);var_dump($a);'

»

Static properties and variable classnames

Oh no!

<?php
class test {
  static public
$foo = 'test1';
  static public
$bar = 'test2';
  function
foo() {
    print
"method foo\n";
  }
}

class
test1 {
  function
__construct() {
    print
"test1 constructor\n";
  }
}
class
test2 {
  function
__construct() {
    print
"test2 constructor\n";
  }
}

function
test1() {
  print
"test1 function\n";
}
function
test2() {
  print
"test2 function\n";
}
function
foo() {
  print
"foo function\n";
  return
'bar';
}

$foo = 'foo';

$a = test::$foo;
new
$a();
new
test::$foo();
$a();
print
test::$foo();
?>

»

On class properties

<?php
class foo  {
  static public
$bar = 2;
}

print
foo::$bar;
$n = new foo;
print
$n->bar;
print
$n->$bar;
?>

Results?

»

_FILES array

An <input type="file" name="foo"> produces this

<?php
array (
 
'foo' =>
  array (
   
'name' => '',
   
'type' => '',
   
'tmp_name' => '',
   
'error' => 4,
   
'size' => 0,
  ),
)
?>

$_FILES array. What do you think foo[bar] will produce?

»

array_slice confused about negative length

Why is the second array below empty? It seems array_splice cannot grok negative length, despite its docs.

<?php
$array
= array('a' => '1');

// GOOD
print_r(array_slice($array, 010));

// WTF. Why empty array?
print_r(array_slice($array, 0,  -10));
?>

Array
(
    [a] => 1
)
Array
(
)

»

Identical arrays vs equal arrays

There is a small problem when comparing arrays -- you can find arrays where array_diff comes back empty and yet, the arrays are not identical.

<?php
$array1
= array(
 
'foo' => 'foo',
 
'bar' => 'bar',
);

$array2 = array(
 
'bar' => 'bar',
 
'foo' => 'foo',
);

var_dump(array_diff($array1, $array2)); // Result: empty array.
var_dump($array1 == $array2); // Result: true.
var_dump($array1 === $array2); // Result: false.
?>

»

Numeric and integer keys

<?php
$a
= 1;
$b ->$a = 'foo';
var_export((array)$b);
var_export(array(1 => 'foo'));
?>

Results:
array (
  '1' => 'foo',
)array (
  1 => 'foo',
)

Observe the '1'. The array element after casting can not be reached via the [] operator. (And no, "'1'" wont do the trick.)

»

Even more hexadecimal fun

Some functions working on floats accept hexadecimal numbers while others don't:

<?php
print sin('15');
print
sin('0xF');
var_dump(round('15'));
var_dump(round('0xF'));
?>

You will see a float(0) for the second one if your PHP version is lower than 5.3.0. In PHP 5.3.0 and ongoing, both of these functions support hexadecimal numbers, however the explicit casting still does not.

Props go to Stan Vassilev for this one and also in general for being a great source of WTFs :)

»

Explicit and implict casting

PHP automatically casts strings to integers when doing arithmetic. However, intval($string) and $string + 0 is not the same! The first one works on numbers only, as described under String conversion to numbers in the manual, however, the latter is happy to accept numeric strings which again means that hexadecimal numbers are accepted. Compare print "0xf"+0 to intval("0xf").

»
Syndicate content