The code listed below is an example showing how to embed PHP into an HTML file:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="GENERATOR" content="Arachnophilia 3.9]"> <meta name="description" content="PHP Example"> <meta name="keywords" content="PHP example"> <title>PHP Example</title> <link href="style.css" rel="stylesheet" type="text/css"> <head> <body> <h1 style="text-align: center">Example</h1> <p> <? echo "This is a test!"; ?> </p> </body> </html>
PHP Execution Order
Server script programs such as Perl will run then the entire output is produced and sent to the client web browser. PHP does not execute like this. PHP executes from the top to the bottom of the page and can be mixed with HTML. This is especially important to be aware of when trying to set cookies in PHP. The example below shows PHP code being run based on a query string. A query string is a string that is appended to a url as follows:
<a href="ctt.php?firstselection">First Selection</a>
The query string that the program will receive in this example is "firstselection", and it is stored automatically in the PHP variable $QUERY_STRING. This example will not show the HTML header. PHP code is shown in blue, and HTML is shown in green.
In this example, if the string is "firstselection", a header of type 1 is used to display the selection. If the string is "secondselection", a header of type 2 is used to display the selection. If no selection or an unknown selection is made, a paragraph is displayed stating that "No selection was made".