Saturday, 5 August 2017

What's this kind of syntax in PHP?



$test= <<
....


EOF;


I have never see it before. What's it used for?


Answer



This is called HEREDOC syntax, which is a way to define strings, on multiple lines, with variable interpolation.




Quoting the manual page:





Heredoc text behaves just like a
double-quoted string, without the
double quotes. This means that quotes
in a heredoc do not need to be
escaped, but the escape codes listed
above can still be used. Variables are
expanded, but the same care must be
taken when expressing complex
variables inside a heredoc as with

strings.




(There is more to read, that I didn't copy-paste from the manual page)




And, as a very quick and simple example:



$a = 'World';
$string = <<



Hello, $a!


MARKER;
echo $string;


It will give you this output:



Hello, World!



And this HTML source:




Hello, World!



No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...