/    Sign up×
Community /Pin to ProfileBookmark

Display PHP inside Echo

I want to display a little php code inside Echo<<< function but I get an error,
this code is just an example. inserting {} is not working.

[code=php]
echo <<<EOD
<html>

<body>

if ($error == 1) {
echo “<center>n<font style=”color: #FF0000″>n”;
foreach($errstr as $err) {
echo “<li> ” . $err . “</li>n”;
}
echo “</font>n</center>n<br />nn”;
}

</body>
</html>
EOD;

[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiApr 26.2010 — If you want to display PHP code as part of an (X)HTML document, place the code in a single-quoted string and use htmlentities() or htmlspecialchars() to output it. You can also use highlight_string() to have the code syntax highlighted.
Copy linkTweet thisAlerts:
@UltimaterApr 26.2010 — I believe you are looking for a nowdoc but they aren't supported 'til PHP 5.3.0:

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

You could store the content in a database or a separate file or use a literal string as Mindzai suggested then all you'd need to escape would be single quotes

or, my personal favorite, use [url=http://us3.php.net/manual/en/function.halt-compiler.php]__halt_compiler[/url] and avoid even having to escape those:

[code=php]
<?php
echo highlight_string(file_get_contents(__FILE__,null,null,__COMPILER_HALT_OFFSET__),true);
__halt_compiler()?>
<?php
if ($error == 1) {
echo "<center>n<font style="color: #FF0000">n";
foreach($errstr as $err) {
echo "<li> " . $err . "</li>n";
}
echo "</font>n</center>n<br />nn";
}
?>
[/code]



Some notes about __halt_compiler:

Either __
halt_compiler()?> or __halt_compiler(); work well but not the combination of __halt_compiler();?> because after PHP sees the semicolon then PHP halts the compiler from that point onwards and the two characters ?> will splash to screen.
Copy linkTweet thisAlerts:
@MindzaiApr 26.2010 — Oops misunderstood. Ignore this.
×

Success!

Help @saud spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...