Doc #69162 [NEW]: ord() returns zero for empty string etc.

From: Date: Mon, 02 Mar 2015 17:33:14 +0000
Subject: Doc #69162 [NEW]: ord() returns zero for empty string etc.
Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-11983@lists.php.net to get a copy of this message
From: salsi at icosaedro dot it Operating system: PHP version: Irrelevant Package: Documentation problem Bug Type: Documentation Problem Bug description:ord() returns zero for empty string etc. Description: ------------ The ord($x) function (see http://www.php.net/function.ord) returns zero for the empty string or anything that can be silently converted to the empty string (NULL, TRUE). Surprisingly, ord($x) also returns 0 if an object is passed, although echo new stdClass() raises E_RECOVERABLE_ERROR: Object of class stdClass could not be converted to string. Either the documentation should state this, or the engine should raise an error, just like it already does when $x is array or resource. I run into this issue while reading the value of a byte read from a file: ord(fread($f,1)) returned 0 on EOF causing a infinite loop. An error would have helped detecting this bug early. Test script: --------------- // unexpected: echo "ord(false): "; var_dump(ord(false)); // int(0) (!) echo "ord(null): "; var_dump(ord(null)); // int(0) (!) echo "ord(''): "; var_dump(ord('')); // int(0) (!) echo "ord(new stdClass()): "; var_dump(new stdClass()); // int(0) (!) // more or less expected: echo "ord(true): "; var_dump(ord(true)); // int(49) echo "ord(123): "; var_dump(ord(123)); // int(49) echo "ord(0): "; var_dump(ord(0)); // int(48) echo "ord(-1): "; var_dump(ord(-1)); // int(45) echo "ord(3.14): "; var_dump(ord(3.14)); // int(51) echo "ord(0.0): "; var_dump(ord(0.0)); // int(48) echo "ord(-1.0): "; var_dump(ord(-1.0)); // int(45) //echo "ord(array()): "; var_dump(ord(array())); // E_WARNING: ord() expects parameter 1 to be string, array given //echo "ord(array(1,2,3)): "; var_dump(ord(array(1,2,3))); // E_WARNING: ord() expects parameter 1 to be string, array given //echo "ord(fopen(__FILE__, 'r')): "; var_dump(ord(fopen(__FILE__, 'r'))); // E_WARNING: ord() expects parameter 1 to be string, resource given Expected result: ---------------- // possible alternative: echo "ord(false): "; var_dump(ord(false)); // E_WARNING: empty string in ord() echo "ord(null): "; var_dump(ord(null)); // E_WARNING: empty string in ord() echo "ord(''): "; var_dump(ord('')); // E_WARNING: empty string in ord() echo "ord(new stdClass()): "; var_dump(new stdClass()); // E_RECOVERABLE_ERROR: Object of class stdClass could not be converted to string // more or less expected: ... -- Edit bug report at https://bugs.php.net/bug.php?id=69162&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=69162&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=69162&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=69162&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=69162&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=69162&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=69162&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=69162&r=needscript Try newer version: https://bugs.php.net/fix.php?id=69162&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=69162&r=support Expected behavior: https://bugs.php.net/fix.php?id=69162&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=69162&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=69162&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=69162&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=69162&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=69162&r=dst IIS Stability: https://bugs.php.net/fix.php?id=69162&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=69162&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=69162&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=69162&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=69162&r=mysqlcfg

« previous php.doc.bugs (#11983) next »