If else embedding inside html
There are a few different ways you can embed PHP code inside HTML.
There are a few different ways you can embed PHP code inside HTML. Here are the standard approaches:
- Standard PHP tags (
<?php ?>): This is the recommended and most widely supported method.
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<?php
if (condition) {
// PHP code to be executed
} else {
// PHP code to be executed
}
?>
</body>
</html>
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Learn object oriented PHP</div>
- Short PHP tags (
<? ?>): These are slightly shorter versions of the standard tags. Important: Short open tags were deprecated in PHP 7.4 and completely removed in PHP 8.0+. They are disabled by default on modern installations. To use them, you must enableshort_open_tag = Onin yourphp.iniconfiguration file. Due to this limitation, the standard<?php ?>syntax is strongly preferred.
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<?
if (condition) {
// PHP code to be executed
} else {
// PHP code to be executed
}
?>
</body>
</html>Both methods allow you to embed PHP logic inside your HTML document. For outputting values directly into HTML, you can also use the short echo tag <?= $variable ?>, which is always enabled by default and works across all PHP versions.