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").

Variable names

A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

Compare to:

<?php
$a
= 1;
$
$a = 'foo';
print ${
1};
?>

So the restriction that the variable name can not start with a number is only enforced by the parser and even then it's fairly easy to circumvent. Maybe it would have been simpler to explain and implement if the definition would be "A valid variable name contains any number of letters, numbers, or underscores".

String comparison

This WTF is documented but a WTF none the less:

If you compare two numerical strings, they are compared as integers.

. You need to add the definition of numerical strings to get the WTF:

Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. Hexadecimal notation (0xFF) is allowed too but only without sign, decimal and exponential part.

. So for example "15" == "0xF".

version_compare

<?php
echo version_compare('anaconda', 'acromantula');
echo
version_compare('baby', 'blister');
echo
version_compare('change', 'cloister');
echo
version_compare('deviant', 'defines');
echo
version_compare('anaconda', 'blister');
echo
version_compare('baby', 'defines');
echo
version_compare('cloister', 'defines');
echo
version_compare('places', 'races');
?>

If you can tell what the output of this is going to be and why then congratulations.

»

Looping

Quoting literally: Note that in PHP the switch statement is considered a looping structure for the purposes of continue.

The beauty of string parsing, parting shot

Did you know that {$array} is tokenized as

<?php
 
array (
   
0 => 'T_CURLY_OPEN',
   
1 => '{',
  ),
  array (
   
0 => 'T_VARIABLE',
   
1 => '$array',
  ),
 
'}',
?>

however ${array} gets parsed into

<?php
 
array (
   
0 => 'T_DOLLAR_OPEN_CURLY_BRACES',
   
1 => '${',
  ),
  array (
   
0 => 'T_STRING_VARNAME',
   
1 => 'array',
  ),
 
'}',
?>

»

The beauty of string parsing part 2

Somebody suggested that I use apostrophes in my string parsing examples. Sure!

<?php
error_reporting
(E_ALL);
$array['test'] = "string used\n";
echo
"$array['test']";
echo
"{$array['test']}";
echo
"${array['test']}";
$array = array();
$array['test'][1] = "string used\n";
echo
"$array['test'][1]";
echo
"{$array['test'][1]}";
echo
"${array['test'][1]}";
?>

So what will happen?

»

The beauty of string parsing

Despite variable parsing in strings is throughly documented (however, there is no direct link like the one here), it is a major WTF.

<?php
error_reporting
(E_ALL);
define('test', 'foo');
$array['foo'] = "constant used\n";
$array['test'] = "string used\n";
echo
"$array[test]";
echo
"{$array[test]}";
echo
"${array[test]}";
$array = array();
$array['foo'][1] = "constant used\n";
$array['test'][1] = "string used\n";
echo
"$array[test][1]";
echo
"{$array[test][1]}";
echo
"${array[test][1]}";
?>

What do you think the result will be when running this?

»
Syndicate content