Doc #69162 [Opn->Nab]: ord() returns zero for empty string etc.
| From: | cmb@php.net | Date: | Tue, 14 Jun 2016 16:51:12 +0000 |
| Subject: | Doc #69162 [Opn->Nab]: ord() returns zero for empty string etc. | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-13527@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69162&edit=1
ID: 69162
Updated by: cmb@php.net
Reported by: salsi at icosaedro dot it
Summary: ord() returns zero for empty string etc.
-Status: Open
+Status: Not a bug
Type: Documentation Problem
Package: Documentation problem
PHP Version: Irrelevant
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
As documented, ord() expects a string. Other types are converted
to string as usual, what is documented in detail in the section
"Converting to string"[1]. Passing an object to ord() results in
a warning, see <https://3v4l.org/uGPeB>. Make sure to
enable error
reporting.
[1] <http://php.net/manual/en/language.types.string.php#language.types.string.casting>
Previous Comments:
------------------------------------------------------------------------
[2015-03-02 17:33:12] salsi at icosaedro dot it
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 this bug report at https://bugs.php.net/bug.php?id=69162&edit=1