Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PHP, how do I express a string literal on multiple lines without the new lines appearing in the rendered text?

Sometimes in PHP when I need to assign a large string literal to a variable, I break the quoted string into multiple lines so it can be read without scrolling 300 characters to the right. My problem is that PHP includes the new-line in the actual string when it is rendered in the application. Is there any way to escape the new line or is there a better way of expressing a string literal on multiple lines? I'm aware that I could use concatenation, but I was hoping for a more elegant solution.

Running on Debian if it matters.

like image 922
leo Avatar asked Jan 30 '12 05:01

leo


People also ask

How do I split a string over multiple lines?

You can have a string split across multiple lines by enclosing it in triple quotes. Alternatively, brackets can also be used to spread a string into different lines. Moreover, backslash works as a line continuation character in Python. You can use it to join text on separate lines and create a multiline string.

Can single quote string support Mutiline in PHP?

No, there's nothing wrong with it at all. Just be aware that the spaces, tabs and line returns in the quoted string will be output as well.

How can you create a multiline echo or assignment in PHP?

Using Double Quotes and Escape Sequences Writing \n or \r\n inside your strings will give you new lines in PHP. This allows you to easily create multiline characters.


1 Answers

On large string i prefer to use the heredoc style, also you can see a couple alternatives in the PHP Documentation about Strings

like image 75
Andrés Torres Avatar answered Oct 06 '22 22:10

Andrés Torres