PHP code is normally mixed with XHTML tags. PHP is an embedded language which means that you can jump between raw HTML code and PHP without sacrificing readability.
In order to embed PHP code with XHTML, the PHP must be set apart using PHP start and end tags. The PHP tags tell the web server were the PHP code starts and ends. The PHP parser recognizes three sets of start and end tags.
The first set of PHP tags is referred to as the XML tag style and is the preferred style. This tag style works with eXtensible Markup Language (XML) documents. This method should be used when mixing PHP with XML or XHTML documents. The examples in this tutorial use this XML tag format.
The short style is the simplest, however, it is not recommended since it interferes with XML document declarations.
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 XHTML editors recognize the XML tag style, this style is not recommended.
These script blocks can be placed anywhere in the XHTML document, at the location at which the script produces and displays its output. The following example XHTML page illustrates use of the three script formats.
<!DOCTYPE html PUBLIC "-//W3C//DTD/XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml11-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <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 previous example three PHP blocks are included in the XHTML document. The first block uses the <php ... ?> opening and closing tags. The code segment uses the PHP echo statement to print the line "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 PHP 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> script block 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 XHTML tags, PHP tags, PHP statements, and whitespace. When a PHP page is requested by a user, any PHP code is processed by the server. When the PHP page is viewed in the browser window, only the text between the opening and closing XHTML 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, a client-side scripting language.