PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP Tags

In PHP, tags are used to enclose the PHP code within a document. The PHP parser looks for the opening and closing PHP tags to know where to start and stop interpreting the PHP code.

There are four different ways to denote PHP tags:

  • Standard PHP tags
<?php
// Your PHP code goes here
?>

The standard PHP tags <?php and ?> are always available regardless of the settings in the PHP.ini file. For maximum compatibility across all PHP installations, it is recommended to use these tags.

  • Short-open (SGML-style) tags
<?
// Your PHP code goes here
?>

Short-open tags are available by default, but they can be disabled using the short_open_tag php.ini configuration file directive. It's not recommended to use short tags because they are not universally supported.

  • ASP-style tags
<%
// Your PHP code goes here
%>

ASP-style tags are removed as of PHP 7.0.0.

  • HTML script tags
<script language="php">
// Your PHP code goes here
</script>

HTML script tags are removed as of PHP 7.0.0.

It's important to note that when using PHP within an HTML document, the PHP code must be enclosed within PHP tags. Everything outside of PHP tags is considered as HTML code by the PHP parser.

Example:

<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>

<?php
echo "Hello World!";
?>

</body>
</html>

In the above example, "Hello World!" is a PHP statement enclosed within PHP tags. This statement will be processed by the PHP parser and replaced with the output of the statement ("Hello World!") in the HTML that's sent to the browser.

  1. PHP opening and closing tags:

    PHP code is enclosed within <?php and ?> tags. For example:

    <?php
    // PHP code goes here
    ?>
    
  2. Short PHP tags vs. full PHP tags:

    Standard (<?php ... ?>) tags are universally supported. Short (<? ... ?>) tags are often discouraged due to configuration issues and potential conflicts.

  3. PHP echo and print tags:

    Both echo and print are used to output data in PHP:

    <?php
    echo "Hello, World!";
    print("Hello, World!");
    ?>
    
  4. Escaping HTML within PHP tags:

    To include HTML within PHP, you can simply write it:

    <?php
    echo "<p>This is an HTML paragraph.</p>";
    ?>
    

    Alternatively, you can escape in and out of PHP:

    <?php
    echo "<p>This is an HTML paragraph.</p>";
    ?>
    
  5. PHP tags and HTML integration:

    PHP and HTML can be seamlessly integrated within the same file:

    <html>
    <body>
        <?php
        $name = "John";
        echo "<p>Hello, $name!</p>";
        ?>
    </body>
    </html>
    
  6. PHP tags in templates and views:

    In template engines or view files, PHP tags are commonly used to embed dynamic content:

    <h1>Welcome, <?php echo $username; ?>!</h1>
    
  7. PHP tags in mixed HTML and PHP files:

    Files with a mix of PHP and HTML often have a .php extension:

    <html>
    <body>
        <?php
        $name = "John";
        echo "<p>Hello, $name!</p>";
        ?>
    </body>
    </html>
    
  8. PHP tags and server-side scripting:

    PHP tags enable server-side scripting, allowing dynamic content generation on the server before sending it to the client's browser.

  9. PHP tags in WordPress and other CMS platforms:

    In WordPress and other CMS platforms, PHP tags are used extensively in theme files and plugins to generate dynamic content.

  10. PHP tags in MVC frameworks:

    In MVC (Model-View-Controller) frameworks like Laravel or CodeIgniter, PHP tags are used in views to display dynamic data.

  11. PHP tags in code editors and IDEs:

    Code editors and IDEs often provide syntax highlighting and auto-completion for PHP tags, making it easier for developers to work with PHP code.