PHP Getting Started

PHP is a very easy language to understand and code in. This article is for those who have a good understanding of HTML and are looking to add more functionality into their website. In order to code with PHP, you must first make sure that your server actually supports PHP. After doing so, you will be able to follow along with the following tutorials.

Basic PHP

Adding PHP code to your HTML document is very similar to just adding an HTML tag. The tag used to tell the server to process the information after it as PHP is:

<?php
The tag used to tell the server to stop processing the information as PHP and return to HTML is:
?>
For more information on how the specifications as to how this works, please read How PHP Works

After we have created our open tag for PHP we need to have some actual code. The most basic example in any programming language is Hello World! To create a webpage that uses PHP to display “Hello World!” we would have the following code.

1.<html>
2.<head><title>Hello World</title></head>
3.<body>
4.<?php
5. print “Hello World!”;
6.?>
7.</body>
8.</html>

How this code works is very simple: Lines 1-3 are basic HTML tags. Lines 7 & 8 are also basic HTML tags. That leaves us with lines 4, 5, & 6. Lets look more closely at those:

4.<?php
5. print “Hello World!”;
6.?>
Line 4 is the open PHP tag. This lets the server know that every line until a ?> should be treated as if it was PHP. Line 6 is this closing tag. So the meat of this is in line 5.

Line 5 has one of the most used commands in PHP: print. The print command takes a string and prints it to the screen. Now we must make sure that what we want to print is in double quotes (“”) and is followed up by a semi-colon (;). The semi-colon tells the PHP engine to stop processing this line and move onto the next. This is very important and can be the cause of many debugging headaches.

Now that we understand how to include PHP into our HTML and how the print function works, lets move onto Using Variables In PHP