php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #73132 mb_substr() doesn't always return a string
Submitted: 2016-09-20 18:04 UTC Modified: 2016-09-20 21:13 UTC
From: greg dot bor at franchisedirect dot com Assigned: cmb (profile)
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: irrelevant
Private report: No CVE-ID: None
 [2016-09-20 18:04 UTC] greg dot bor at franchisedirect dot com
Description:
------------
---
From manual page: http://www.php.net/function.mb-substr
---
"Return Values" says this returns a string, which is true for most cases but not true for at least one edge case: `var_dump(mb_substr(array(), 0, 1));` returns NULL in PHP 5.3+ (but returns "A" before PHP 5.3). 

I haven't checked the PHP source, I guess these it can be determined when exactly does mb_substr return NULL.

Test results with https://3v4l.org/CuDLX:

Output for 5.3.0 - 5.4.45, 5.5.0 - 5.5.34, 5.5.35 - 5.6.26, hhvm-3.10.0 - 3.14.4, 7.0.0 - 7.1.0RC2
`
Warning: mb_substr() expects parameter 1 to be string, array given in /in/CuDLX on line 3
NULL
`

Output for 4.3.0 - 5.2.17
`
Notice: Array to string conversion in /in/CuDLX on line 3
string(1) "A"
`

Test script:
---------------
var_dump(mb_substr(array(), 0, 1));

Expected result:
----------------
The documentation should define when does mb_substr returns NULL.

Actual result:
--------------
The documentation suggests mb_substr always returns a string.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-09-20 21:13 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2016-09-20 21:13 UTC] cmb@php.net
The NULL return value is not special to mb_substr(), as the
documentation about internal functions already states[1]:

| If the parameters given to a function are not what it expects,
| such as passing an array where a string is expected, the return
| value of the function is undefined. In this case it will likely
| return NULL but this is just a convention, and cannot be relied
| upon.

[1] <http://php.net/manual/en/functions.internal.php>
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Sep 19 17:01:25 2020 UTC