Best Practices: Working with Long, Multiline Strings in PHP

Best Practices: working with long, multiline strings in PHP?

You should use heredoc or nowdoc.

$var = "some text";
$text = <<<EOT
Place your text between the EOT. It's
the delimiter that ends the text
of your multiline string.
$var
EOT;

The difference between heredoc and nowdoc is that PHP code embedded in a heredoc gets executed, while PHP code in nowdoc will be printed out as is.

$var = "foo";
$text = <<<'EOT'
My $var
EOT;

In this case $text will have the value "My $var", not "My foo".

Notes:

  • Before the closing EOT; there should be no spaces or tabs. otherwise you will get an error.
  • The string/tag (EOT) that enclose the text is arbitrary, that is, one can use other strings, e.g. <<<FOO and FOO;
  • EOT : End of transmission, EOD: End of data. [Q]

Multi-line strings in 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";

PHP Writing long strings on separate lines

If SQL syntax highlighting is your problem, you can probably format it properly using a heredoc, and still get the IDE's syntax highlighting working:

$query = <<<EOD
select *
from foo
where bar
EOD;

do_query($query);

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

If you don't want line breaks, your way of doing it now seems right. With the above, you'd need to remove the line breaks afterwards, e.g. str_replace("\n", " ", $str).

Saving multiline strings that contain variables, into a file with PHP and CodeIgniter

You can use str_replace

$plain_text = "define host {
use template;
host_name $host_name;
address 46.224.2.220;
contacts $email;
}";

$find = array("$host_name", "$email");
$replace = array($host_name, $email);

$new_plain_text = str_replace($find, $replace, $plain_text);

PHP - String spanning multiple lines

You can use this format:

$string="some text...some text...some text...some text..."
."some text...some text...some text...some text...some text...";

Where you simply use the concat . operator across many lines - PHP doesn't mind new lines - as long as each statement ends with a ;.

Or

$string="some text...some text...some text...some text...";
$string.="some text...some text...some text...some text...";

Where each statement is ended with a ; but this time we use a .= operator which is the same as typing:

$string="some text...some text...some text...some text...";
$string=$string."some text...some text...some text...some text...";

Issues with table td expanding on long strings of texts

you can reach this by adding display: inline-block;
I created fiddle http://jsfiddle.net/QMCMA/ to demonstrate this

Textmate: Auto indenting long line of PHP code

Can you try View->Soft Wrap or View->Wrap Columns?

You should try to not have super long strings in your code because it makes it hard to read for other people (especially those that put their monitors 90 degrees). A lot of times, if I have to have a super long string in the PHP like that, I'll do something like this:

echo 'This may be a long line of code, which will wrap to multiple '
. 'lines depending on the width of the browser window...';

And if I need to put a bunch of text into a variable for some reason, like a raw SQL query:

$query = 'SELECT id, name, other_column, this_property, something_else '
. 'FROM really_long_table_name_for_no_reason '
. 'WHERE other_column IN ['item1', 'item2', 'item3', 'item4'] '
. 'AND this_property = 7 '
. 'AND something_else = 'nine_why_not' '
. 'ORDER BY id DESC LIMIT 30,90';

Create variable from text string with new lines and quotes

How about this, which allows you to use unescaped quote marks in your text block:

$str = <<<DATA
saldflasdfl

asdklksadlasd "quoted string"

aslkdlsadfkl
DATA;

Note that it does need to be added with no whitespace indentation, since that would be taken to be part of the string.

However, do note that if your text block gets over a certain size, it is probably cleaner to put it in a text file and read it into a string.



Related Topics



Leave a reply



Submit