php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #20695 substr does not always return a string
Submitted: 2002-11-28 04:40 UTC Modified: 2002-12-09 05:44 UTC
From: RNova at gmx dot net Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 4.2.3 OS: Windows 98
Private report: No CVE-ID: None
 [2002-11-28 04:40 UTC] RNova at gmx dot net
substr($s,$n,$m) is supposed to always return a string (at least if $s is a string and $n and $m are integers).

However:

   is_string(substr($s,strlen($s),$m)) 

returns false, so substr did not return a string.

Example:
$s="<AB attr = \n\"value\"  >";
echo is_string("")."<BR>\n";
echo "substr(\"A\",0,1):".is_string(substr("A",0,1))."<BR>\n";
echo "substr(\"A\",0,0):".is_string(substr("A",0,0))."<BR>\n";
echo "substr(\"A\",1,1):".is_string(substr("A",1,1))."<BR>\n";
echo "substr(\"A\",1,0):".is_string(substr("A",1,0))."<BR>\n";

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-11-28 04:45 UTC] RNova at gmx dot net
substr($s,$n,$m) is supposed to always return a string (at least if $s
is a string and $n and $m are integers).

However:

   is_string(substr($s,strlen($s),$m)) 

returns false, so substr did not return a string.

Example:
echo is_string("")."<BR>\n";
echo "substr(\"A\",0,1):".is_string(substr("A",0,1))."<BR>\n";
echo "substr(\"A\",0,0):".is_string(substr("A",0,0))."<BR>\n";
echo "substr(\"A\",1,1):".is_string(substr("A",1,1))."<BR>\n";
echo "substr(\"A\",1,0):".is_string(substr("A",1,0))."<BR>\n";
 [2002-11-28 04:49 UTC] jan@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php4-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php4-win32-latest.zip

jan@dahlia ~> php bug_20695.php
1<BR>
substr("A",0,1):1<BR>
substr("A",0,0):1<BR>
substr("A",1,1):<BR>
substr("A",1,0):<BR>
jan@dahlia ~> php -v
PHP 4.4.0-dev (cli)

works fine here

 [2002-12-08 10:20 UTC] sniper@php.net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.


 [2002-12-09 05:18 UTC] RNova at gmx dot net
Hi Jan,

thanks for you reply! I have tried the snapshot
(for Windows) that you pointed me to. The program
works as you say - which IMHO is a bug.

The output is - as you mention:

1<BR>
substr("A",0,1):1<BR>
substr("A",0,0):1<BR>
substr("A",1,1):<BR>
substr("A",1,0):<BR>

This means that substr("A",1,1) and substr("A",1,0) are both
not strings. Therefore substr does _not_ always return a string.
If this is considered correct, then it should be mentioned in
the documentation. 

Personally, I would prefer this to be considered
incorrect: I would prefer substr($s,$n,$m) to always return a string, 
at least if $s is a string and $n and $m are integers.

Please either improve the documentation or make substr($s,$n,$m) 
always return a string (at least if $s is a string and $n and $m are integers).

Thanks for putting this PHP together. It is a great means to produce results quickly!

Cheers,
	Raimund
 [2002-12-09 05:43 UTC] sniper@php.net
This script shows clearly what happens:

<?php

var_dump(substr("A",0,1));
var_dump(substr("A",0,0));
var_dump(substr("A",1,1));
var_dump(substr("A",1,0));

?>

Output:

string(1) "A"
string(0) ""
bool(false)
bool(false)

And this is quite correct and expected behaviour.
Documentation though should mention that invalid start position and length will make it return FALSE.

 [2002-12-09 05:44 UTC] sniper@php.net
..and that's what the docs say too.. :)

 [2004-06-23 21:16 UTC] mauroi at digbang dot com
What if I make the following call to substr ?
var_dump(substr('', 0));

The documentation says "If string is less than start characters long, FALSE will be returned".
That can be very ambiguous because start is 0 and the length of the string is 0 (not less).
Another problem: mb_substr does not work like this. If you overload the functions and make the same call as above it will return an empty string.

Thanks in advance.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 17 13:01:27 2024 UTC