Modular Websites

Creating a Basic PHP Page

Open up a blank TextWrangler file and write:

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

This is a legitimate HTML5 document, because you will recall that the html, head, title and body tags are optional.

Save the file on the server, and when you open it in your browser, you will see "Hello World!" in the upper left hand corner.

You have just executed a php script and created an HTML page. If you look at the code, all the php will have disappeared. Only the HTML is left.

A few things to note about the PHP script. First, the statement starts with <?php and ends with ?>. That is how you format the PHP code, which is written between these brackets.

The statement ends in a semicolon, just like CSS statements do. That signifies to the PHP processor that the statement is complete.

The text is surrounded by quotes just like the attribute values in the HTML tag. This is because we are writing code, and content is something that the code works on, and has to be differentiated from the code. This is especially true of text, since there is a conflict in the way that regular writing uses certain characters, like space and return for example, and so it is separated from the code with quotes. HTML is the other way around, where all of the code is separated from the content by tags, because it's really about the content, while a coding language like PHP is really about the code.

The last thing you see is the word echo. This is a language construct, and it is what makes PHP Hypertext friendly. Use echo to turn PHP code into HTML.