Support Forums
PHP Multiline Strings - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: PHP The Hypertext Preprocessor (https://www.supportforums.net/forumdisplay.php?fid=21)
+---- Thread: PHP Multiline Strings (/showthread.php?tid=2767)

Pages: 1 2


PHP Multiline Strings - Gaijin - 11-10-2009

Ohhh man I see those in almost every script, and everyone does it wrong.

You have 2 options to use multiline strings.

PHP Code:
$str "
Your
multiline
string "


But that's bad.
Let me show you a quite better way to write multiline strings within PHP, it's called heredoc and nowdoc.

heredoc
Heredocs behave as strings inside double quotes ("), but the double quotes doesn't need to be escaped ( \" ).
The heredoc is defined with <<<, following by a indentifier and a new line.

http://www.php.net Wrote:
Warning

It is very important to note that the line with the closing identifier must contain no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter (possibly followed by a semicolon) must also be followed by a newline.

The variables within an heredoc statement are best to be put inside of a pair of { and }

The syntax for a heredoc looks like:

PHP Code:
$str = <<<ANYTHING
You can write everything you {$want}
in as many lines as you want.
ANYTHING; 

Please read more


RE: PHP Multiline Strings - zone - 11-10-2009

This is new to me, thankyou Ninja !


RE: PHP Multiline Strings - Gaijin - 11-10-2009

(11-10-2009, 03:17 AM)zone Wrote: This is new to me, thankyou Ninja !

Np mate, heredoc is better for formatting long strings within PHP.


RE: PHP Multiline Strings - Spl3en - 11-11-2009

Thanks for this tips, it's really useful for making a code easy to read.
Another manner to do that, a bit more "proper" is to use chr(10) IMHO.
Example :
PHP Code:
<?php
$multiline 
chr(10);
echo 
"Hello".$multiline."This is multilines!";
?>



RE: PHP Multiline Strings - wat - 11-17-2009

(11-11-2009, 04:45 PM)Spl3en Wrote: Thanks for this tips, it's really useful for making a code easy to read.
Another manner to do that, a bit more "proper" is to use chr(10) IMHO.
Example :
PHP Code:
<?php
$multiline 
chr(10);
echo 
"Hello".$multiline."This is multilines!";
?>

How is this more proper? the chr() function returns the ascii value of a number, so how does calling a function to get the ascii value of 10 (I'm guessing it's the newline char, as that's the only thing that would make sense in this case) a more proper way of implementing a multiline string?

What you did makes use of a needless variable, and it is harder to understand what you were trying to accomplish.

Another possible way of implementing newline's is '\n', which would look something like:

PHP Code:
echo "lol \n new line"

However, \n's aren't really an alternative to heredoc's as they aren't really friendly to your eyes.

Think about it, which one makes more sense?

PHP Code:
$intro "Hello, sir!\ntoday we will be offering you lots of stuff.\nyou can get a tv\na computer\nor a new desk!\nchoose now.\n"

or...

PHP Code:
$intro = <<<EOL
Hello, sir!
today we will be offering you lots of stuff.
you can get a tv
a computer
or a new desk!
choose now.
EOL; 

Sorry, I kinda rambled there. In short, chr() should definitely not be used for implementing newlines within a string ;).

Also, nice guide Master Of The Universe.


RE: PHP Multiline Strings - Gaijin - 11-17-2009

^^ Ohhh man thanks, I didn't view this thread in a while.

Yes using "\n" is way more better than chr(10), but that will not do anything to the structure of the output, it will spread into multiple lines only in the sourcecode only as long as \n is placed inside of double-quotes.

But for output it's still only choise to use <br /> to make the new line in the browser.


RE: PHP Multiline Strings - wat - 11-17-2009

Ahh, sorry, I didn't realize I was bumping an old thread.

And yeah, <br /> for new lines in the browser, but \n will be sufficient for newlines when your output isn't being interpreted by a browswer ;).


RE: PHP Multiline Strings - Gaijin - 11-17-2009

(11-17-2009, 08:34 PM)FarOut Wrote: Ahh, sorry, I didn't realize I was bumping an old thread.

And yeah, <br /> for new lines in the browser, but \n will be sufficient for newlines when your output isn't being interpreted by a browswer ;).

Nah don't worry....I mean I didn't seen Spl3en's post.
And yes you're right.


RE: PHP Multiline Strings - Omniscient - 11-17-2009

Good point. I really rarely use these but given this thread I think I will more often. I know of many instances this will be better than the basic string in quotes with escapes.


RE: PHP Multiline Strings - Gaijin - 11-17-2009

I use heredoc for every string (mostly HTML one), I never worked with nowdoc's but heredoc is much better for long strings then placing them within quotes.