STEIKE
PHP MUST DIE

A comparison of how different scripting languages treat their users.

/tmp$ perl -le 'print 07'
7
/tmp$ perl -le 'print 08'
Illegal octal digit '8' at -e line 1, at end of line
Execution of -e aborted due to compilation errors.

/tmp$ python -c 'print 07' 
7
/tmp$ python -c 'print 08'
  File "", line 1
    print 08
           ^
SyntaxError: invalid token

/tmp$ php -r 'print 07' 

Parse error: parse error in Command line code on line 1 /tmp$ php -r 'print 07;' 7/tmp$ /tmp$ php -r 'print 08;' 0

Comments

Rick Hull — 2005-04-01 00:50 php -r 'print 07'; -- parse error for no semicolon

php -r 'print 07;' -- prints '7'. any integer preceded by 0x is interpreted as hex. any integer preceded by 0 is interpreted as octal. see http://ch2.php.net/manual/en/language.types.integer.php

php -r 'print 08;' -- prints '0'. invalid octal number

alf — 2005-04-01 23:30 Yes, I know perfectly well that 08 is invalid. My point was that PHP should treat it as such, and not fail silently. A less idiotic thing to do would be to give the user a warning, or fail. Just using the wrong number is dangerous; the user might think this works after checking a few values:

$magic_dates = array(stamp(01,01,1980),
   stamp(12,31,1980),
   stamp(08,11,1950)); # fails silently!

PHP doesn't report that anything is wrong even at the most pedantic warning level:

error_reporting(E_ALL); print 028;  # prints 2 without warning

Why? Laziness, ignorance or both. PHP only supports octal numbers by accident, as evidenced by the following snippet from zend_language_scanner.l:

<ST_IN_SCRIPTING>{LNUM} {
        errno = 0;
        zendlval->value.lval = strtol(yytext, NULL, 0);
        if (errno == ERANGE) { /* overflow */
                zendlval->value.dval = zend_strtod(yytext, NULL);
                zendlval->type = IS_DOUBLE;
                return T_DNUMBER;
        } else {
                zendlval->type = IS_LONG;
                return T_LNUMBER;
        }
}

strtol() sees the first 0, decides to use octal, and eats as much of the number as it can. Because PHP completely ignores the endptr argument, it throws away the rest of the number.

More suckage:

error_reporting(E_ALL);  # doesn't help :)
foreach(array(0xffffff,
              0xfffffff,
              0xffffffff,
              0xfffffffff,
              0xffffffffff) as $x) {
  printf("%s (%s)\n", $x, gettype($x));
}

Output:

16777215 (integer)
268435455 (integer)
4294967295 (double)
2147483647 (integer)
2147483647 (integer)

Rick Hull — 2005-04-05 05:53 Oh I see. I should read more carefully..

You're absolutely right about failing silently.

/me sits in the corner

anonymous — 2005-05-19 01:54 You are so right! I have felt that PHP sucks for awhile. It is little things like that, that really make me hate that sorry excuse for a language.