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"
}

»

php_uname is "consistent"

According to the manual: php_uname returns a description of the operating system PHP is running on. On Unix, the output reverts to displaying the operating system information PHP was built on if it cannot determine the currently running OS.

There is no mention of when it cannot determine the currently running OS nor how to figure out whether it was or not able to..

It's quite logical...

<?php
$a
= 0;
$b = 'x';
var_dump(FALSE == $a);
var_dump($a == $b);
var_dump($b == TRUE);
?>

This results in
bool(true)
bool(true)
bool(true)

if you were to follow simple rules learned in elementary school (or just common sense) then seemingly FALSE == TRUE. Opsie.

»
Syndicate content