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
andFOO;
- 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
Laravel 4: How to "Order By" Using Eloquent Orm
How to Count the Numbers of Rows That a MySQL Query Returned
Request Headers Bag Is Missing Authorization Header in Symfony 2
Woocommerce:Add Custom Metabox to Admin Order Page
How to Test If a User Has Selected a File to Upload
Tcpdf Utf-8 Symbols Not Showing Up
PHP Printed Boolean Value Is Empty, Why
Aws Cloudfront + Load Balancer, Url Changes from Main Domain to Load Balancer Subdomain
Check If a Row Exists Using Old MySQL_* API
Laravel 5.2 Not Reading Env File
Passing PHP Objects to JavaScript
PHP - Import CSV File to MySQL Database Using Load Data Infile
Is HTMLentities() Sufficient for Creating Xml-Safe Values
Codeigniter Csrf - How Does It Work