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?

»

More incrementing and decrementing

<?php
for ($i = 'a'; $i <= 'z'; ++$i) echo "$i ";
?>

I do not even know what's a bigger WTF -- that you can increment characters and they do increment like characters instead of being cast to integers (just try $a = 'a'; $a += 1;) or that z is followed by aa or that the loop does not stop there. Of course, those who read the previous entry and took the effort to learn the incrementing/decrementing rules are less surprised because they read

»

Incrementing and decrementing

<?php
// I just need four NULLs to demo this.
$a = array_fill(0, 4, NULL);
$a[0]++;
++
$a[1];
$a[2]--;
--
$a[3];
var_dump($a);
?>

array(4) {
  [0]=>
  int(1)
  [1]=>
  int(1)
  [2]=>
  NULL
  [3]=>
  NULL
}

No, I do not know why. It's just WTF. The documentation has a note on this but that does not make this less WTF. And adding a fifth NULL and then $a[5] -= 1 makes it -1...

»

Emptiness and arrays

Today's WTF is more than one:

<?php
$a
= array();
$a[''] = 'foo';
$a[] = 'foo';
var_dump($a);
?>

results in
array(2) {
  [""]=>
  string(3) "foo"
  [0]=>
  string(3) "foo"
}

»
Syndicate content