How PHP and HTML mix

PHP is text in a file on a Web server. The text gives instructions to the server on how to create an HTML page. One clever thing about PHP is that you can mix it with HTML so your HTML and CSS skills can still be used. As an example this Web page includes PHP code:

            
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>Hello world as text</title>
    </head>
    <body>                
        <?php                
            echo "<p>Hello world</p>";                
        ?>        
    </body>
</html>            
        

You should recognise a standard HEAD section but within the BODY is your first PHP script. You can insert PHP anywhere in a Web page by starting it with <?php and ending it with ?>. There are variations but this is the correct way.

The code above is just one line. ECHO sends the text in quotes to the Web page. The line ends with a semi-colon just like JavaScript.

Remember that the PHP code is never seen by the Web browser as it is interpreted/run on the server. When the page gets to the browser it will look like a standard HTML page where the BODY will look like this:

            
<body>        
    <p>Hello world</p>
</body>            
        

To tell the Web server to look inside a Web page for PHP code you need to save it with a .php extension. Paste this code (the top block) and save it, for now, as firstphppage.html. Double click on it and it will open in your browser. You should see that the PHP has not been processed. It also doesn't show because the browser just ignores tags which it doesn't understand. In the browser view the source of the page (right click on the page or use the View menu in most browsers). You should see the PHP code there.

Change the file extension for firstphppage to .php. Now when you double click on it now you hit another major problem. You don't have a server! This makes it impossible for you to test your PHP pages. You need to get one.

Before carrying on with these pages make sure you have a way to get your files to a server for testing (the three main ways to test your PHP pages).

Once you have got the above PHP page uploaded to a server you should load it in a browser. Look at the source code in your browser to see what the HTML looks like (right click and View Source or similar). You should not see the PHP code as that was interpreted on the server and resulted in HTML.

Every time you create a new PHP page insert the HTML, HEAD and BODY elements from your HTML template unless the page will never be seen by users.

If you ever have a page which you have uploaded but which refuses to work with an error about permission being denied check that the uploaded file (the copy on the server) is owned by www-data (for Apache servers).

submit to reddit Delicious Tweet