php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #66303 Inline Heredoc / Nowdoc
Submitted: 2013-12-17 01:29 UTC Modified: 2013-12-17 22:52 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: childerkc at gmail dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.5.7 OS: CentOS Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2013-12-17 01:29 UTC] childerkc at gmail dot com
Description:
------------
I use heredoc and nowdoc at times, and feel they are very powerful if you know why your using them. With that, I wish they were sexier when it came to the syntax, I am not a fan of commands to go in and out of a heredoc, being on their own line. This tends to get annoying if you are trying to pass a bunch of different pieces of data to different parameters of a function. An example could be found in the test script below

Test script:
---------------
// Obviously not the most logical function in this case, but point still holds true, heredocs are ugly and don't need to be...
function foo($css,$js,$html) {handleCSS($b);handleJS($r);return $html;}

if($someTrueCondition) {
    echo foo(
<<<'CSS'
       #someID{width: 100px; height: 100px}
CSS
    , // <---------- :(
<<<'JS'
       alert("simple alert");
JS
    , // <---------- :'(
<<<'HTML'
       <div id="someID">$SomeOtherInnerHTML</div>
HTML
    );
}

// The above code just makes me want to cry... such lonely random commas...

// Why instead, can this not be simplified? I have seen some other bug requests that just make the heredocs more complicated.. my goal is to not make them any more complicated, just to simplify the usage of them for us developers who do use them [maybe more then we should]... My Proposal is to simply allow them to be inline like the rest of the code? And instead, maybe simply allow them follow the flow of the developers ohh so beautiful code.. so..

// The only thing one would need to do is to instead make sure that the start of a heredoc is at the END OF A LINE, and the END of a heredoc, is at the START of a line, EXCLUDING ALL DATA BEFORE START TAG AND AFTER END TAG... So the above ugliness, becomes a little easier on the eyes:

if($someTrueCondition) {
    echo foo(<<<'CSS'
       #someID{width: 100px; height: 100px}
    CSS, <<<'JS'
       alert("simple alert");
    JS, <<<'HTML'
       <div id="someID">$SomeOtherInnerHTML</div>
    HTML);
}

// And if an ending heredoc starts on a line, then breaks out of heredoc, it can then go back in to it, on the same line, at the END of the line... I would also say that the heredoc should then handle removing any white space in the content for each line that is equivalent to the whitespace before the END tag for that heredoc... This would keep the code beautiful and the end result beautiful.. (maybe even leave the content alone, white space and all, no worries for me as something cleans if up for me later anyway)..

// What I would LOVE to see, is the heredoc in an actual INLINE way... So that the above 2 ugly's become the sexy code below... This would mean the syntax would probably need to change a little, start tag would be something simple like <<<'EOD'> and the end tag would be the opposite without the quotes <EOD>>>, everything between the > and < would adhere to the heredoc standards...

if($someTrueCondition) {
    echo foo(
        <<<'CSS'>#someID{width: 100px; height: 100px}<CSS>>>,
        <<<'JS'>alert("simple alert");<JS>>>,
        <<<'HTML'><div id="someID">$SomeOtherInnerHTML</div><HTML>>>
    );
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-17 02:20 UTC] mail at requinix dot net
Inline heredoc and nowdoc... So you mean normal string syntax? Surely
  '#someID{width: 100px; height: 100px}'
is "sexier" than
  <<<'CSS'>#someID{width: 100px; height: 100px}<CSS>>>
 [2013-12-17 22:40 UTC] childerk at gmail dot com
Ok ok you got me there, maybe I went a little overboard with my example, but it was simply to show that if you have many of them, the ugliness accumulates. 

My real main issue, is when done with some chunk of data that obviously has use of "" and '' inside it, heredoc is a brilliant solution, as I don't need to backslash every little thing, and where I use heredoc's are not to get large chunks of data in, but smaller pieces, and so my documents, in cases where I use this, look ridiculous. I don't understand why it is done this way in the first place? Nothing else in PHP, AFAIK, forces its syntax to be on a new line, why would a heredoc? an if(could){$be_Done_This_Way;}, in a single line, why not a heredoc? :/
 [2013-12-17 22:47 UTC] childerkc at gmail dot com
And now that I re-look at my example, I didn't even put use of both " and ' inside the heredoc example code, so imagine some single quotes in there too.. So those that come here and feel the need to comment that, well I was tired.. if you understand how and why to use a heredoc, well I am glad, we are on the same page, I simply would like to see them no longer forced to a single line to start and end one, why can these not be [inline] with the rest of the code?
 [2013-12-17 22:52 UTC] aharvey@php.net
-Status: Open +Status: Wont fix
 [2013-12-17 22:52 UTC] aharvey@php.net
Allowing the end token to appear as part of a larger line would make it far easier for heredocs to be ended accidentally by the end token appearing in the heredoc content, the <<<>>> option is veering very close to symbol soup, and since single and double quotes can span multiple lines, I don't think there's much benefit here.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 02 06:01:28 2024 UTC