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.
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.
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.
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.
On large string i prefer to use the heredoc style, also you can see a couple alternatives in the PHP Documentation about Strings
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With