Page Content

Tutorials

How To Use PHP Tags In HTML?

PHP Tags In HTML

PHP tags are unique markers that allow PHP code to be embedded into other file types, most frequently HTML documents. Telling the web server the PHP code begins and ends is their main purpose. Any text between these opening and closing tags is subsequently processed by the PHP interpreter. Outside of these tags, all text or markup is handled as plain HTML and sent to the web client in its original form.

The PHP interpreter starts running the contained code when it comes across an opening PHP tag (). When the PHP code block is executed, its output if any replaces it in the final document that is provided to the browser. Only the final output, usually HTML, is visible to the user’s browser; the raw PHP code is not displayed.

Generally, a file must contain a.php extension in order for the web server to process it as a PHP script. PHP code may not be executed by the web server if PHP tags are included in a file with a different extension (such as.html).

The describe several styles of PHP tags:

PHP Tags
PHP Tags

Standard (XML) Style: The most widely used and successful tag style is the standard (XML) style. The format is . The inability of the server administrator to disable this style ensures that it is available on all servers. It works with both XHTML and texts written in the Extensible Markup Language (XML). Much of the programming literature uses this tag style.

Short Style: The abbreviation for these tags is they adhere to the format of a processing instruction written in the Standard Generalised Markup Language (SGML). Although they are the quickest to type, it is not advised to use them. This is because they need to be enabled in the php.ini configuration file or PHP must be compiled with a special option, as they are disabled by default in many circumstances. Additionally, because are discouraged by PSR-1 coding rules since they are frequently deactivated.

Short Echo Tag: The format for this tag is .It is used to directly echo material and is a shorthand for . All PHP versions support this tag, and since PHP 5.4, it is always enabled, independent of the short_open_tag parameter. Its availability was frequently associated with short_open_tag being enabled prior to PHP 5.4.0. The appropriate use of brief echo tags is one of the recommended practices outlined in the PSR-1 standard. Variables are frequently output using this manner when embedded in HTML.

Script Style: is another accepted tag style. This format could be used since some editors, such as FrontPage, disliked processing instructions like <?php.

ASP Style: <%… %> is used in these tags. They imitate Active Server Pages’ (ASP) syntax. You usually need to enable a configuration option in the php.ini file in order to use this style. Also mentioned as a shortcut for <% echo… %> is <%=… %>.

Although there are several different tag formats, the PHP always advise using the short echo tags and the normal tags for optimal compatibility and compliance with current coding standards.

Simple statements inside PHP tags are usually ended with a semicolon (;). The semicolon preceding the closing tag is optional, though, as the concluding tag?> also suggests the conclusion of a sentence. Although it’s not required, using a semicolon is frequently seen as best practice.

Outside of PHP tags, whitespace such as tabs, carriage returns, and spaces is handled as normal HTML and transmitted straight to the browser. Although the PHP interpreter typically ignores whitespace inside PHP tags that are not inside string literals or comments, it is essential for improving the readability of your code.

Additionally, coding standards like PSR advise against using the closing?> element in files that exclusively contain PHP code. This procedure avoids unintentionally producing whitespace after the ending tag, which could lead to problems, especially when working with headers or when the output is parsed by client programs.

Code example showing standard and short echo tags and PHP embedded in HTML:

<!DOCTYPE html>
<html>
<head>
    <title>PHP Tag Example</title>
</head>
<body>
    <h1>Demonstrating PHP Tags</h1>
    <?php
        // This is a PHP statement
        $message = "Hello from standard PHP tags!"; // Semicolon required
        /*
         * This is a multi-line PHP comment.
         * Whitespace and comments within the tags are ignored by the interpreter.
         */
        // Another statement using echo
        echo "<p>" . $message . "</p>"; // Semicolon required
        // A simple calculation
        $sum = 5 + 3; // Semicolon required
    ?>
    <p>Here is some plain HTML text.</p>
    <?php // Standard tag example ?>
    <p>The sum of 5 and 3 is: <?= $sum ?></p> <?php // Short echo tag example, note no semicolon needed before this closing tag ?>
    <p>This line follows the short echo output.</p>
    <?php
        // In a file with only PHP, the closing tag is often omitted
        // echo "This is the last line in PHP."; // Semicolon required if this were the only content before the tag
    ?>
    <?php echo "This is the last line in PHP."; ?> // Semicolon used here before the closing tag
</body>
</html>

When the web server processes this file, the PHP code within the and tags is executed. The browser will receive HTML with PHP code blocks replaced by echo and output. Without tags, plain HTML will be transmitted. Final output would look like:

<!DOCTYPE html>
<html>
<head>
    <title>PHP Tag Example</title>
</head>
<body>
    <h1>Demonstrating PHP Tags</h1>
    <p>Hello from standard PHP tags!</p>
    <p>Here is some plain HTML text.</p>
    <p>The sum of 5 and 3 is: 8</p>
    <p>This line follows the short echo output.</p>
    This is the last line in PHP.
</body>
</html>

Only the generated HTML output appears in the browser’s view, not the PHP code.

PHP Works with HTML

PHP is a server-side programming language created especially for the internet. Building web apps and producing dynamic web content are its main uses. HTML files provide the ability to directly include PHP code. Blocks of PHP code within a file are denoted by special PHP tags, most frequently . Text that is not enclosed in these tags is transmitted straight to the browser as plain HTML. A web server processes a file that ends in.php by running the code contained between the PHP tags when it gets a request for the file. Before the server sends the finished document to the browser, the PHP script is replaced with the output produced by the PHP code, such as when echo or print are used. Only the generated HTML is sent to and rendered by the user’s browser.

<html>
<head>
<title>Sample PHP Script</title>
</head>
<body>
<p>The following prints "Hello, World":</p>
<?php
print "Hello, World";
?>
</body>
</html>
``` [9, 17, 18, 20, 49-52]
Index