|  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
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.
Block user comment
Status: Assign to:
Bug Type:
From: greg dot bor at franchisedirect dot com
New email:
PHP Version: OS:


 [2016-09-20 18:04 UTC] greg dot bor at franchisedirect dot com
From manual page:
"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

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

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.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-09-20 21:13 UTC]
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2016-09-20 21:13 UTC]
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] <>
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Sep 28 10:01:24 2020 UTC