portia Posted July 7, 2011 Share Posted July 7, 2011 I'm a newbie reading php documentations. I have a hard time grasping the idea/use of heredoc strings. That's an example from the php website $str = <<<EOD Example of string spanning multiple lines using heredoc syntax. EOD; I understand it is useful when you have multiline string variables. Is that it? Is this the only advantage of heredoc strings or there's more to it? thanks Quote Link to comment https://forums.phpfreaks.com/topic/241320-use-of-heredoc-strings/ Share on other sites More sharing options...
AyKay47 Posted July 7, 2011 Share Posted July 7, 2011 on of the main reasons for using the HEREDOC syntax is it eliminates the need to escape special characters inside of it, and allows for neater strings Quote Link to comment https://forums.phpfreaks.com/topic/241320-use-of-heredoc-strings/#findComment-1239628 Share on other sites More sharing options...
portia Posted July 7, 2011 Author Share Posted July 7, 2011 Thanks for your reply! Quote Link to comment https://forums.phpfreaks.com/topic/241320-use-of-heredoc-strings/#findComment-1239753 Share on other sites More sharing options...
xyph Posted July 7, 2011 Share Posted July 7, 2011 Keep in mind HEREDOC strings are parsed (like a double quoted string). If you don't need/want them parsed, use a NOWDOC. echo <<<HEREDOC \x41 HEREDOC; // Outputs 'A' echo '<br>'; echo <<<'NOWDOC' \x41 NOWDOC; // Outputs '\x41' Quote Link to comment https://forums.phpfreaks.com/topic/241320-use-of-heredoc-strings/#findComment-1239781 Share on other sites More sharing options...
Pikachu2000 Posted July 7, 2011 Share Posted July 7, 2011 It's important to note that NOWDOC syntax isn't available before PHP v5.3.0, however. Quote Link to comment https://forums.phpfreaks.com/topic/241320-use-of-heredoc-strings/#findComment-1239786 Share on other sites More sharing options...
portia Posted July 7, 2011 Author Share Posted July 7, 2011 Thanks guys, I'm aware of that difference between heredoc and nowdoc. Just wasn't sure about the general idea of it. Quote Link to comment https://forums.phpfreaks.com/topic/241320-use-of-heredoc-strings/#findComment-1239817 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.