How to echo in PHP, HTML tags
<?php
echo '<div>
<h3><a href="#">First</a></h3>
<div>Lorem ipsum dolor sit amet.</div>
</div>
<div>';
?>
Just put it in single quotes.
How can I echo the actual HTML tags with PHP?
Use htmlentities
to render the p
tags as text.
echo htmlentities('<p>' . $t . '</p>');
echo '<br>';
Use the PHP_EOL
constant to add a new line character the html ouput.
echo '<p>' . $t . '</p>';
echo PHP_EOL;
How do I echo html tags in PHP Variable?
HTML tags will automatically parsed by the browser. To see the pure HTML source anyways, the tags needs to be encoded, especially the angle brackets.
There is a dedicated PHP function available for that called htmlentities().
$var = "Hello<br>How are <strong>you</strong>";
echo htmlentitites($var);
Will encode it in the browser so you can see it displayed as plain text like in $var
. The browser will see this:
Hello<br>How are <strong>you</strong>
How can I echo HTML in PHP?
There are a few ways to echo HTML in PHP.
1. In between PHP tags
<?php if(condition){ ?>
<!-- HTML here -->
<?php } ?>
2. In an echo
if(condition){
echo "HTML here";
}
With echos, if you wish to use double quotes in your HTML you must use single quote echos like so:
echo '<input type="text">';
Or you can escape them like so:
echo "<input type=\"text\">";
3. Heredocs
4. Nowdocs (as of PHP 5.3.0)
Template engines are used for using PHP in documents that contain mostly HTML. In fact, PHP's original purpose was to be a templating language. That's why with PHP you can use things like short tags to echo variables (e.g. <?=$someVariable?>
).
There are other template engines (such as Smarty, Twig, etc.) that make the syntax even more concise (e.g. {{someVariable}}
).
The primary benefit of using a template engine is keeping the design (presentation logic) separate from the coding (business logic). It also makes the code cleaner and easier to maintain in the long run.
If you have any more questions feel free to leave a comment.
Further reading is available on these things in the PHP documentation.
NOTE: PHP short tags <?
and ?>
are discouraged because they are only available if enabled with short_open_tag
php.ini configuration file directive, or if PHP was configured with the --enable-short-tags
option. They are available, regardless of settings from 5.4 onwards.
How do I echo a php variable inside html tags inside an if statement?
If you want to echo PHP variable inside echo
function, you must as first break echo with "
, then add a "merge char" .
, then your variable and then again "merge char" and "
.
Also don't forget to add semicolon to end of your line which contains definition of $_SESSION['age']
.
Edit your code to:
<?php
$_SESSION['age'] = 11;
if($_SESSION['age'] < 14) {
echo "<h2>Your only ".$_SESSION['age']."? Thats Young!</h2>";
}
?>
How can I echo tags as plain text within a PHP loop?
You can use <
for < and >
for >
Like this:
<?php
while ($row = mysqli_fetch_array($execute)) {
echo "<name>".$row['name']."</name>";
}
?>
Or like this:
<?php
while ($row = mysqli_fetch_array($execute)) {
echo htmlspecialchars("<name>".$row['name']."</name>");
}
?>
Print HTML tags in PHP output
Just use
$str = "A 'quote' is <b>bold</b> ";
echo htmlspecialchars($str);
because htmlentities
convert some characters to HTML entities.
You should instead use htmlspecialchars
It replaces characters as below:
'&' (ampersand) becomes &
'"' (double quote) becomes " when ENT_NOQUOTES is not set.
"'" (single quote) becomes ' only when ENT_QUOTES is set.
'<' (less than) becomes <
'>' (greater than) becomes >
You can check php fiddle here
Related Topics
Regex to Strip Comments and Multi-Line Comments and Empty Lines
Long Polling/Http Streaming General Questions
Compile a Referenced Less File into CSS with PHP Automatically
How to Force PHP to Use Strings for Array Keys
How to Remove the Url Protocol and Slash from User Input in PHP
Pdo Fetchall Array to One Dimensional
How to Delete Files from Directory Based on Creation Date in PHP
How to Decrypt a Password Hash in PHP
Why I Can Not Login to Magento Backend Using Google Chrome
PHP Variable Interpolation VS Concatenation
Variable Position in Comparision in PHP
Pass Arraylist Bean from Android to Webservice PHP
Find First Character That Is Different Between Two Strings
Programmatically Add Product to Cart with Price Change
Setting Up Laravel on a MAC PHP Artisan Migrate Error: No Such File or Directory