|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79762 ord should throw ValueError exception on empty string
Submitted: 2020-07-01 00:21 UTC Modified: 2020-07-01 07:43 UTC
From: raincomplain at outlook dot com Assigned:
Status: Open Package: Strings related
PHP Version: 8.0.0alpha1 OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2020-07-01 00:21 UTC] raincomplain at outlook dot com
Empty string in PHP doesn't equal U+0000 which is why mb_ord will not return 0 when passing empty string. On the other hand, ord will not complain about passing empty string. I know ord is not aware of any string encoding but this behavior however leads to a wrong assumption that empty string === U+0000 which in turn leads to bugs like

So one might think if 

var_dump(ord('') === 0); // true


var_dump(ord("\u{0000}") === 0); // true

Then the following must be true

var_dump("\u{0000}" === ''); // false

Test script:

Expected result:
Fatal error: Uncaught ValueError: ord(): Argument #1 ($str) must not be empty

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-07-01 01:13 UTC]
-Status: Open +Status: Feedback
 [2020-07-01 01:13 UTC]
Throwing an exception seems rather extreme. Is there a problem with returning false like many other functions already do?
 [2020-07-01 01:32 UTC] raincomplain at outlook dot com
ord will return non-Boolean value which evaluates to FALSE 

ord("\u{0000}") // 0

This makes returning false sub-optimal, anyhow mb_ord already throws ValueError so I think it would be proper for ord to throw an exception just like mb_ord.
 [2020-07-01 07:43 UTC]
-Status: Feedback +Status: Open
 [2020-07-01 07:43 UTC]
I agree that the behavior of both functions should ideally match,
but mb_ord('') threw a warning already before PHP 8, while ord('')
did not.  So maybe it would be best for BC to let ord('') raise a
warning and return FALSE in PHP 8.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 14:01:31 2024 UTC