This question already has answers here:
Calling PHP functions within HEREDOC strings
(17 answers)
Use a variable within heredoc in PHP
(1 answer)
Closed 8 years ago.
I have been stuck trying to write PHP code inside some HTML tags I set up.
I have a variable inside a .php file called $content, and I have a HTML page code inside this variable in the following way:
$content = <<<HTML some html code in here HTML;
Now, within that HTML tag I wanted to add some PHP code, tried using <?php ..... ?>, however I wasn't successful with it. Can anyone help?
That's a HEREDOC syntax , The closing HTML; should be in margin else it wont work.
Valid Syntax
<?php
$somevar="Hello World";
echo <<<HTML
<b> Hey this is some text and I am adding this variable $somevar</b>
HTML;
Invalid Syntax
<?php
$somevar="Hello World";
echo <<<HTML
<b> Hey this is some text and I am adding this variable $somevar</b>
HTML; //<--- As you can see there is a leading space
You can always use <script type="text/php">...</script>
With very old version of PHP, HEREDOC <<<HTML to open a string are not always supported.
If you use an old PHP, a workaround is to do:
$myvar="some html code here link
next line of html code
".$myotherphpvar."
rest of lines"
Note that if you open string with ", you must escape all " for html code with \.
With recent PHP version you can use HEREDOC syntax. This is example of syntax:
$myvar=<<<HTML
some html code here link
next line of html code $myotherphpvar
rest of lines
HTML;
More examples on page:
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
Related
This question already has answers here:
new lines in PHP. how?
(3 answers)
how to write new lines in CLI and web browser?
(5 answers)
PHP - how to create a newline character?
(15 answers)
Closed 2 years ago.
Sample PHP code
wolf#linux:~$ cat 1-new-line.php
<?php
echo "Hello
World
Foo Bar!";
$aString = '
First Line
Second Line
Third Line';
echo $aString;
?>
wolf#linux:~$
It runs as expected in CLI
wolf#linux:~$ php 1-new-line.php
Hello
World
Foo Bar!
First Line
Second Line
Third Line
wolf#linux:~$
But not via browser
What's wrong in this code? How to render it properly in web browser?
Browsers interpret PHP's output as HTML, and in HTML newlines get replaced by spaces.
If you want to learn how to line-break in a browser, use <br /> or wrap sentences in <p> tags.
You can also tell a browser to treat your output as text, and not HTML. In that case, use the following header (before any output):
header('Content-Type: text/plain');
You are rendering it in a browser without any HTML formatting. Wrapping it in a <pre></pre> tags will preserve its plaintext formatting (newlines and whitespace) when rendered in the browser.
<pre>
<?php
echo "Hello
World
Foo Bar!";
</pre>
$aString = '
First Line
Second Line
Third Line';
<pre>
echo $aString;
</pre>
?>
I need the same html-text in different places of my script so I want to save my html text in a php variable. Kinda like this:
HTML:
<div>
<p>bla</p>
</div>
-->php:
$html="<div>\n\t<p>bla</p>\n</div>";
There are already some tools like this which transforms html text into multiple lines of php echo code. Is there also a tool for a more compact solution like in my example?
You can use NOWDOC (available since PHP 5.3.0) formatting to store the text:
$html = <<<'HTML'
<div>
<p>bla</p>
</div>
HTML;
If you want to use variables in your text, then you can use HEREDOC instead:
$html = <<<HTML
<div>
<p>{$variable}</p>
</div>
HTML;
See also: Advantages / inconveniences of heredoc vs nowdoc in php
I'm using Sublime Text with the Pastels on Dark theme. My language of choice is PHP. How can I get HTML syntax highlighting inside PHP strings & heredoc syntax?
Name your heredocs after the language you are using. This will syntax highlight in many text editors, including Sublime Text.
For example:
echo <<<HTML
<!-- put HTML here and it will have syntax highlighting -->
HTML;
Wanted to add this as a comment to Ol' Reliable's answer but I am not allowed yet.
Whilst coding outside and then copying in can be a hassle, for people/editors without syntax highlighting in heredoc, an easy workaround is to temporarily add a closing php tag to the heredoc opening tag:
<?php
$myHtmlCode = <<<HTML?>
<h1>I am Highlighted</h1>
<p>Remove the closing php tag above to finish editing</p>
HTML;
?>
Just code outside of php so you can still see the HTML syntax color-coded, and then put that html inside the php once you are done.
This question already has answers here:
PHP - how to create a newline character?
(15 answers)
Closed 9 years ago.
I've tried every method to output a newline in PHP. Why doesn't the following work? :
<?php
$foo = 'bar';
echo "Hello \n $foo!";
?>
This should output a newline between hello and bar but it isn't.
I also tried \r\n instead of \n
If you using it as command line script this would work. I would use PHP_EOL for this since it chooses the right line break for the OS.
However if you are working with HTML (viewing the result in browser for example) you have to use the HTML way of linebreaks which is: <br />
To output a new line in HTML you need to use HTML's representation of a new line which is <br/>
php has a function for you that converts all natural new lines to HTML new lines > nl2br()
Then your code should look like
<?php
$foo = 'bar';
echo nl2br("Hello \n $foo!");
?>
Use <br> when inserting line break in html
To output a new line visually (in a browser), you need HTML:
echo "Hello\n<br />$foo!";
\n is a system line feed.
Try this:
<?php
$foo = 'bar';
echo "Hello <br> $foo!";,
?>
if you view the source you will see it actually DOES have a new line in it, But white space doesnt matter in HTML (what the browser displaying the output is expecting) but adding a <br /> to your statement will produce a new line because this is the way browsers read new lines and display them.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Have I misunderstood what heredoc should do?
I read that Here document preserves the line breaks
and other whitespace (including indentation) in the text. But when I run the following script,everything gets printed on the same line. Why is it so ?
<?php
$str = <<<HDC
This is a sample text
Some more sample text
Even more sample text
HDC;
echo $str;
The output actually does contain the line breaks. However, HTML (by default) ignores line breaks.
If you want the HTML to render the line breaks, wrap it in a pre:
<pre>
<?php
$str = <<<HDC
This is a sample text
Some more sample text
Even more sample text
HDC;
echo $str;
?>
</pre>
Or if you cannot use <pre> because <pre> always prints in fixed width, manually insert the <br> tags.