php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #69162 ord() returns zero for empty string etc.
Submitted: 2015-03-02 17:33 UTC Modified: 2016-06-14 16:51 UTC
From: salsi at icosaedro dot it Assigned: cmb (profile)
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: salsi at icosaedro dot it
New email:
PHP Version: OS:

 

 [2015-03-02 17:33 UTC] 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:
...


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-14 16:51 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2016-06-14 16:51 UTC] cmb@php.net
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>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 21:01:30 2024 UTC