Multi-Line Strings in PHP

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

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]

PHP multiline string with PHP

You don't need to output php tags:

<?php 
if ( has_post_thumbnail() )
{
echo '<div class="gridly-image"><a href="'. the_permalink() .'">'. the_post_thumbnail('summary-image', array('class' => 'overlay', 'title'=> the_title('Read Article ',' now',false) )) .'</a></div>';
}

echo '<div class="date">
<span class="day">'. the_time('d') .'</span>
<div class="holder">
<span class="month">'. the_time('M') .'</span>
<span class="year">'. the_time('Y') .'</span>
</div>
</div>';
?>

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

Using a php code in a variable with multiple line strings

Heredoc expands variables itself. You don't need the whole <?php echo $topic_image ?>, just the variable:

$display_content=<<<END_OF_TEXT
<div>
<img src="image/${topic_image}" style="width:200px;height:150px;">
<p style="float:left">$topic_pris</p>
<p style="float:left">$topic_title</p>
<p style="float:left">$topic_name</p>
</div>
END_OF_TEXT;

Multi-Line String to an Array

Use file():

$arrayX = file(
'http://domain.tld/file-path/',
FILE_IGNORE_NEW_LINES
);

For reference, see:

  • http://php.net/manual/en/function.file.php

Finding multi-line string in PHP file

Rather than trying to parse a PHP file in PHP, depending on what you're needing to do, you might be able to use var_export().

require_once($filename);

// variables are now in global scope
// manipulate as necessary
$first_array['this_key'] = 'that value';

$str = '$first_array = '.var_export($first_array, TRUE).";\n\n";
$str .= '$second_array = '.var_export($second_array, TRUE).';';

// output the updated arrays back to the file
file_put_contents($filename, $str);


Related Topics



Leave a reply



Submit