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


Messages In This Thread
PHP Multiline Strings - by Gaijin - 11-10-2009, 02:35 AM
RE: PHP Multiline Strings - by zone - 11-10-2009, 03:17 AM
RE: PHP Multiline Strings - by Gaijin - 11-10-2009, 11:36 AM
RE: PHP Multiline Strings - by Spl3en - 11-11-2009, 04:45 PM
RE: PHP Multiline Strings - by wat - 11-17-2009, 08:17 PM
RE: PHP Multiline Strings - by Gaijin - 11-17-2009, 08:30 PM
RE: PHP Multiline Strings - by wat - 11-17-2009, 08:34 PM
RE: PHP Multiline Strings - by Gaijin - 11-17-2009, 08:40 PM
RE: PHP Multiline Strings - by Omniscient - 11-17-2009, 09:39 PM
RE: PHP Multiline Strings - by Gaijin - 11-17-2009, 09:50 PM
RE: PHP Multiline Strings - by Spl3en - 11-18-2009, 03:09 PM

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 807 07-27-2020, 11:26 PM
Last Post: tk-hassan
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,333 10-10-2011, 01:00 PM
Last Post: Greyersting

Forum Jump:


Users browsing this thread: 1 Guest(s)