php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42790 strrchr() doesn't work as expected with heredoc string containing blank line
Submitted: 2007-09-28 16:48 UTC Modified: 2007-10-02 12:31 UTC
From: mahesh dot vemula at in dot ibm dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5CVS-2007-09-28 (snap) OS: RHEL 4, Windows XP
Private report: No CVE-ID: None
 [2007-09-28 16:48 UTC] mahesh dot vemula at in dot ibm dot com
Description:
------------
strrchr() function can't find the new line when an heredoc string containing only a blank line is given as input, whereas it works fine when the heredoc string containing regular string with new line.

This is noticed on php5 and php6.

Reproduce code:
---------------
<?php
$str1 = <<<EOD

EOD;

$str2 = <<<EOD
hello
hiiiiiii
EOD;

//doesn't work as expected
var_dump( strrchr($str1, $str1) );
var_dump( strrchr($str1, "\n") );
var_dump( strrchr($str1, "
") );

//works as expected
var_dump( strrchr($str2, "\n") );
var_dump( strrchr($str2, "
") );
?>


Expected result:
----------------
string(1) "
"
string(1) "
"
string(1) "
"
string(9) "
hiiiiiii"
string(9) "
hiiiiiii"


Actual result:
--------------
bool(false)
bool(false)
bool(false)
string(9) "
hiiiiiii"
string(9) "
hiiiiiii"


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-02 12:31 UTC] tony2001@php.net
See this simple example:
<?php
$str1 = <<<EOD

EOD;
var_dump($str1);
?>

This code outputs 'string(0) ""', which explains why strrchr() or any other function will not find any characters including newline in there.
The data in heredocs starts AFTER the initial new line and finished BEFORE the ending newline, so it looks like this: 
[START_DELIMITER and newline]data[newline and END_DELIMITER].
There is no data in your string, so strrchr() behaves correct.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 18:01:35 2025 UTC