Multi-line strings in PHP

PhpString

Php Problem Overview


Consider:

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

echo $xml;

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

Php Solutions


Solution 1 - Php

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";

Solution 2 - Php

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.

Solution 3 - Php

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.

Solution 4 - Php

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

Will echo:

l
vv

Documentation on PHP_EOL.

Solution 5 - Php

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.

Solution 6 - Php

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.

Solution 7 - Php

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

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

will give you "lvv";

Solution 8 - Php

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;

Solution 9 - Php

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

echo $xml;

But you should really look into http://us3.php.net/simplexml

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTheBlackBenzKidView Question on Stackoverflow
Solution 1 - PhpMadara's GhostView Answer on Stackoverflow
Solution 2 - PhpDan FabulichView Answer on Stackoverflow
Solution 3 - PhpeselkView Answer on Stackoverflow
Solution 4 - PhpRyanView Answer on Stackoverflow
Solution 5 - PhpXavi EsteveView Answer on Stackoverflow
Solution 6 - Phpbikash pandaView Answer on Stackoverflow
Solution 7 - PhpNorbert OrzechowiczView Answer on Stackoverflow
Solution 8 - PhpShawn SolomonView Answer on Stackoverflow
Solution 9 - PhpnathanjosiahView Answer on Stackoverflow