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 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.
echo HTML with php variable
Try this.. run in your php enviroment
<html>
<head>
<style type="text/css">
p
{
text-align: center;
color: red;
}
</style>
</head>
<body>
<?php
$var = "Hello World";
echo "<p>The value of the variable is : " . $var . "</p>";
?>
</body>
</html>
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 to use php echo without quotes to echo html code?
I don't know where the quotes are coming from - the code you have in your question doesn't add extra quotes so they are coming from somewhere else.
However if you want the HTML string to be rendered as HTML instead of displaying the tags as text, you can do the following:
Starting with this value in your variable:
<h1 class="ql-align-center">TEST</h1>
displayed as: <h1 class="ql-align-center">TEST</h1>
...you can use html_entity_decode
to decode it which will give us the following output, i.e. it converts it into a string that will display as plain text HTML when you echo it:
<h1 class="ql-align-center">TEST</h1>
displayed as: <h1 class="ql-align-center">TEST</h1>
...now we need to decode this to turn it into the HTML elements that will be displayed as a H1 tag in the page:
<h1 class="ql-align-center">TEST</h1>
displayed as: TEST
Code: To do this, you need to call html_entity_decode
twice before it will display the string as HTML elements:
<?php
$htmlstr = html_entity_decode($singleEmail['camp_desc'], ENT_QUOTES, 'UTF-8');
echo html_entity_decode($htmlstr, ENT_NOQUOTES, 'UTF-8');
?>
How to echo result in list using php
Generate the HTML in the for
loop
<?php
$query="select name from plant ";
$q=mysqli_query($con,$query)or die("Could Not Perform the Query");
$hh = ''; //empty string first
while ($row = $q->fetch_assoc())
{
$hh .= '<li><a href="#">' . $row['name'] . '</a></li>';
// ^--------------------- concat with the previous result
}
?>
<ul id="myUL">
<?php echo $hh; /* display */ ?>
</ul>
PHP Echo to a certain part of HTML
You can assign the text to a variable and use PHP down inside your HTML to echo that.
<?php
session_start();
if (isset($_POST['username']) && isset($_POST['password'])) {
include_once("db.php");
$username = mysqli_real_escape_string($sqlcon, $_POST['username']);
$username = strtoupper($username);
$password = mysqli_real_escape_string($sqlcon, $_POST['password']);
for ($i = 0; $i < 1000; $i++) {
$password = hash(sha512, $password . $username);
}
$userQuery = "SELECT * FROM users WHERE username = '" . $username . "' LIMIT 1";
$user = mysqli_query($sqlcon, $userQuery);
if (mysqli_num_rows($user) > 0) {
$user = mysqli_fetch_assoc($user);
$id = $user['id'];
$databasepass = $user['password'];
if ($password === $databasepass) {
$_SESSION['username'] = $username;
$_SESSION['id'] = $id;
header("Location: admin.php");
} else {
$result = "The username and password you entered did not match our records. Please double-check and try again.";
}
} else {
$result = "The username and password you entered did not match our records. Please double-check and try again.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>ADMIN</title>
</head>
<body>
<div>
<?php echo $result; ?>
</div>
</body>
</html>
Related Topics
How to Validate an Email in PHP
How to Get the Current Date and Time in PHP
Detect Browser Language in PHP
Calling Closure Assigned to Object Property Directly
How to Get the Jquery $.Ajax Error Response Text
How to Store My Users' Passwords Safely
Reading/Writing a Ms Word File in PHP
Why Can't I Run Two MySQLi Queries? The Second One Fails
Best Way to Get Result Count Before Limit Was Applied
PHP Domdocument Loadhtml Not Encoding Utf-8 Correctly
PHP Generate File For Download Then Redirect
How to Load Classes Based on Pretty Urls in MVC-Like Page
What Does a \ (Backslash) Do in PHP (5.3+)