Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP Multiline Strings
#1
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
Reply
#2
This is new to me, thankyou Ninja !
Reply
#3
(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.
Reply
#4
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!";
?>
Reply
#5
(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.
Reply
#6
^^ 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.
Reply
#7
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 ;).
Reply
#8
(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.
Reply
#9
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.
Superman I am here to rescue you.
This is Support Forums not Support PMs.  Do not PM me for support unless it's private and site related.
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 778 07-27-2020, 11:26 PM
Last Post: tk-hassan
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,273 10-10-2011, 01:00 PM
Last Post: Greyersting

Forum Jump:


Users browsing this thread: 1 Guest(s)