|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2009-10-22 15:53 UTC] svn@php.net
[2009-10-22 15:54 UTC] rquadling@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Fri Jun 19 04:00:02 2026 UTC |
Description: ------------ The documentation for substr() does not seem to adequately describe PHP's behavior when the length parameter is not given, or is FALSE or NULL. When length is not given, the entire string, beginning from the position specified by start, is returned. With most PHP functions, using NULL in place of a parameter means that it will be treated as if the parameter were not given at all; substr() does not seem to obey this, and this fact is not documented. Instead of acting as if length were not given, it acts as if length were 0, and returns an empty string. Additionally, if length is FALSE, it is treated as if length were given as 0, and an empty string returned again. This was surprising behavior; for instance, it prevented me from using a call to strpos() as the third parameter to substr() and getting the desired result when strpos() returns FALSE, that is, the entire string instead of an empty string, which was the actual result. I filed this as "Documentation problem" because I would like to see the documentation reconciled with PHP's actual behavior. But perhaps the NULL issue, at least, is something that should be fixed in PHP? Reproduce code: --------------- var_dump(substr("hello", 0)); var_dump(substr("hello", 0, 0)); var_dump(substr("hello", 0, NULL)); var_dump(substr("hello", 0, FALSE)); var_dump(substr("hello", 0, strpos("hello", "?"))); Expected result: ---------------- string(5) "hello" string(0) "" string(5) "hello" string(5) "hello" string(5) "hello" Actual result: -------------- string(5) "hello" string(0) "" string(0) "" string(0) "" string(0) ""