Web Development Tutorials




  
  

Mixing HTML and PHP

PHP code is normally mixed with HTML tags. PHP is an embedded language, meaning that you can jump between raw HTML code and PHP without sacrificing readability.

In order to embed PHP code with HTML, the PHP must be set apart using PHP start and end tags. The PHP tags tell the web server where the PHP code starts and ends. The PHP parser recognizes three styles of start and end tags.

XML Style

<?php
  PHP Code Block
?>         

The first style of PHP tags is referred to as the XML tag style and is preferred most. This tag style works with eXtensible Markup Language (XML) documents. This method should be used especially when mixing PHP with XML or HTML documents. The examples in this tutorial use this XML tag format.

Short Style

<?
PHP Code Block
?>

The short style is the simplest, however, it is not recommended since it could interfere with XML document declarations.

Short Style

<script language="php">
  PHP Code Block
<script> 

This style is the longest and is similar to the tag style used with JavaScript. This style is preferred when using an HTML editor that does not recognize the other tag styles. Since most new HTML editors recognize the XML tag style, this style is not recommended.

NOTE: The most common style for denoting a PHP code block is the XML style. You will see it most often when working with PHP. This is the style that will be used throughout this tutorial. These script blocks can be placed anywhere in the HTML document, at the location at which the script produces and displays its output. The following example HTML page illustrates the use of the three script formats.

 
 <!DOCTYPE html>
<html
<head>
<title>A Web Page</title>
</head>
<body>

<p>
        
<?php
 echo "This is a basic PHP document";
?>
            
</p>

<p>

<?
 print "PHP is fun!";
?>

</p>

<p>
        
<script language="php">
 $myVar = "Hello World";
echo $myVar;  

</script>
            
</p>

</body>
</html>
         

In the example three different styles of PHP blocks are included in the HTML document. The first block uses the <?php ... ?> opening and closing tags. The code segment uses the echo statement to print the text, "This is a basic PHP document", to the browser window.

>

The second block uses the <? ... ?> tags to mark the begining and end of the PHP code. This section uses the print statement, an alias for echo, to display the text "Hello World" to the screen.

Finally, the third block uses the <script language="php"> ... </script> style to designate the beginning and end of the PHP code. Here the string "Hello World" is assigned to variable $myVar and the echo statement displays the value of $myVar to the browser window.

This is a basic PHP page PHP is fun! Hello World

The example code above includes HTML tags, PHP tags, PHP statements, and whitespace. When a PHP page is requested through a web browser by the user, PHP code is processed by the PHP engine in the web server. Thus, when the PHP page is rendered in the browser window, only the text between the opening and closing HTML or PHP tags is shown. None of the actual PHP code is visible when viewing the source code from the browser window. This is because the PHP interpreter runs through the script on the server and replaces the code with the output from the script. Only the output is returned to the browser. This is one of the characteristics that make PHP a server-side scripting language unlike JavaScript, which is a client-side scripting language.


TOP | NEXT: Displaying Content