TheBlackBenzKid
TheBlackBenzKid

Reputation: 27087

Multi-line string literals in PHP

Consider:

$xml = "l";
$xml = "vv";

echo $xml;

This will echo vv. Why and how can I do multi-line string literals for things like SimpleXML, etc.?

Upvotes: 260

Views: 435507

Answers (10)

Tom Károly
Tom Károly

Reputation: 11

Other technique (easy way)

Creating variable:

$lines = array(
   "l",
   "vv"
);
$xml = '';
foreach($lines as $line) $xml .= $line . PHP_EOL;

Printing:

$lines = array(
   "l",
   "vv"
);
foreach($lines as $line) echo $line, PHP_EOL;

Upvotes: 1

Dan Fabulich
Dan Fabulich

Reputation: 39563

PHP has Heredoc and Nowdoc strings, which are the best way to handle multiline strings in PHP.

http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
$var is replaced automatically.
EOD;

A Nowdoc is like a Heredoc, but it doesn't replace variables.

$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
$var is NOT replaced in a nowdoc.
EOD;

You don't have to use "EOD" as your start/end identifier; it can be any string you want.

Beware indentation. Prior to PHP 7.3, the end identifier EOD must not be indented at all or PHP won't acknowledge it. In PHP 7.3 and higher, EOD may be indented by spaces or tabs, in which case the indentation will be stripped from all lines in the heredoc/nowdoc string.

Upvotes: 330

bikash panda
bikash panda

Reputation: 111

PHP has two inbuilt methods HEREDOC and NOWDOC to handle multiline strings. HEREDOC has also it syntax <<<.

Here is the example of multiline PHP string by using HEREDOC,

<?php
echo <<<EOT
My name is BB. I am printing some Text.
Now, I am printing SECOND LINE.
This should print a capital 'A': \x41
EOT;
?>

Note: If you want to check the proper output of the above code by using your local server, you have to run the complete code on CMD or XAMPP Shell.

Upvotes: 11

Xavi Esteve
Xavi Esteve

Reputation: 1162

Another solution is to use output buffering, you can collect everything that is being outputted/echoed and store it in a variable.

<?php
ob_start(); 

?>line1
line2
line3<?php 

$xml = ob_get_clean();

Please note that output buffering might not be the best solution in terms of performance and code cleanliness for this exact case but worth leaving it here for reference.

Upvotes: 11

eselk
eselk

Reputation: 6884

Not sure how it stacks up performance-wise, but for places where it doesn't really matter, I like this format because I can be sure it is using \r\n (CRLF) and not whatever format my PHP file happens to be saved in.

$text="line1\r\n" .
      "line2\r\n" .
      "line3\r\n";

It also lets me indent however I want.

Upvotes: 19

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174957

Well,

$xml = "l
vv";

Works.

You can also use the following:

$xml = "l\nvv";

or

$xml = <<<XML
l
vv
XML;

Edit based on comment:

You can concatenate strings using the .= operator.

$str = "Hello";
$str .= " World";
echo $str; //Will echo out "Hello World";

Upvotes: 422

Shawn Solomon
Shawn Solomon

Reputation: 111

To put the strings "l" and "vv" on separate lines in the code alone:

$xml = "l";
$xml .= "vv"
echo $xml;

In this instance you're saying to append .= the string to the end of the previous version of that string variable. Remember that = is only an assignment operator so in your original code you're assigning the variable a new string value.

To put the strings "l" and "vv" on separate lines in the echo alone:

$xml = "l\nvv"
echo $xml;

You don't need multiple strings in this instance, as the new line character \n will take care of that for you.

To put the strings "l" and "vv" on separate lines in code and when echoing:

$xml = "l";
$xml .= "\nvv"
echo $xml;

Upvotes: 2

Ryan
Ryan

Reputation: 28187

$xml="l" . PHP_EOL;
$xml.="vv";
echo $xml;

Will echo:

l
vv

Documentation on PHP_EOL.

Upvotes: 12

nathanjosiah
nathanjosiah

Reputation: 4459

$xml="l\rn";
$xml.="vv";

echo $xml;

But you should really look into https://www.php.net/simplexml

Upvotes: 1

Norbert Orzechowicz
Norbert Orzechowicz

Reputation: 1311

Maybe try ".=" indead of "="?

$xml="l";
$xml.="vv";

will give you "lvv";

Upvotes: 3

Related Questions