W3docs

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:

  1. 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"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

  1. 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 enable short_open_tag = On in your php.ini configuration 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.