To be or not to be, that is the question.

<?php
use not\existing;

print
existing::class
?>

»

String comparison is utter broken

<?php
function compare($a, $b, $c) {
 
var_dump($a < $b);
 
var_dump($b < $c);
 
var_dump($c < $a);   
}
?>

What do you call this function with to get:

bool(true)
bool(true)
bool(true)

»

String indexes do not cast

I can't even.

php > print 2[0];
PHP Parse error:  syntax error, unexpected '[' in php shell code on line 1
php > $a=2;
php > print $a[0];
php > $a='2';
php > print $a[0];
2
php > $a=2;
php > print $a{0};
php >

Sure, the syntax error makes sense but the rest? Of course substr($a, 0, 1) works always.

Variable function calls

<?php
$callable
= array($x, $y);
call_user_func($callable);
$callable();
?>

call_user_func works. However, $callable() does not. Even on PHP 5.4 and later when the two supposed to be identical. How can that can be?

»

Objects with __toString are not strings.

<?php
class foo {
  function
__toString() {
    return
"x";
  }
}
$foo = new foo;
var_dump(strpos($foo, "x"));
var_dump(strpos("x", $foo));
$array = array("x" => "y");
var_dump($array[$foo]);
var_dump($foo[0]);
?>

What's the output?

»

list() is behaving logical and weird at the same time

<?php
list($a, $b) = array(1, 2, 3, 4);
?>

What direction will list start processing the array? You'd expect that it'd be left to right and that's right: $a = 1, $b = 2. But where it gets confusing:

<?php
list($a, $a) = array(1, 2, 3, 4);
?>

Well, we already know list will only consider 1 and 2. So first 1 gets assigned to $a and then 2 gets assigned and that's where our story ends, right? After all, the second $a is where $b used to be, right? Wrong. The manual warns you

»

Default arguments and type hinting

Run these three:

php -r 'function a(array $a){}; a(NULL);'
php -r 'function a(array $a = NULL){}; a(NULL);'
php -r 'function a(array $a = FALSE){}; a(FALSE);'

»

The poor parser is easily confused

PHP can just cast numbers to strings, right?

<?php
print "a"."2";
?>

<?php
print "a".2;
?>

Results
a2

PHP Parse error:  syntax error, unexpected '.2' (T_DNUMBER) in Command line code on line 1

instanceof is smart

<?php
'$a="foo";
var_dump($a instanceof stdClass);
var_dump("foo" instanceof stdClass);
?>

»

NULL is a data type and is not a scalar

While NULL is listed as a data type the consequences of this is not triivial. Most importantly, this is the reason why it is not considered a scalar. is_scalar($variable) is a shorthand for is_bool($variable) || is_string($variable) || is_int($variable) || is_float($variable) and NULL is none of those. It "feels" like NULL should be a scalar -- Wikipedia says "In computing, a scalar is any non-composite value".

»
Syndicate content