PHP Tutorial: Prerequisites and basic syntax

1) Installation

Because PHP is a server-side language, you need to install it first- not like HTML where it comes “pre-installed” in the clients’ browser.  Installing PHP can be quite tricky, but if you’re paying a web-host to host your website for you, it’ll be installed anyway.  If you don’t have access to a live server, use a development environment like XAMPP (recommended) that comes with PHP installed already.

2) Webpage extension

Any page that you want to put PHP in must have the .php extension, instead of the standard .html or .htm.  It’s probably best to get into the habit of creating all pages with this extension, regardless or whether or not there’s actually PHP on it.  .php pages behave in exactly the same way as normal .html pages do, and if all you want is HTML, you don’t need to do anything different.

3) Containing tags

For PHP to work, the code you write must be surrounded by PHP tags, like this:

<?php

//some code

?>

Without these tags the server doesn’t know that what you’re writing is PHP, and will treat it as if it were HTML text that you wanted to display on-screen. As a note of interest, a “shorthand” version also exists, as shown below. However it’s recommended that you steer clear of this- partly because it serves no purpose other than to save you typing two extra characters, and also because you have to specially configure the server to accept it, so it may not work on a different machine- PHP is supposed to be universal.

<?

//probably best not to do this

?>

4) Commenting

Just like HTML, PHP also supports commenting. This is how you’d code a comment in HTML:

<html>

<!--This is a comment-->

</html>

…and this is how it’s done in PHP:

<?php

//This is a comment

?>

Now the above example shows you how to create a single-line comment. This means that the slashes only comment out the line on which they’re used, unlike the HTML version. So if you want to create a comment that runs over multiple lines, you have to do it this way:

<?php

/*
This
is a
comment
*/

?>

The semicolon

I guess you could say that the semicolon is equivalent to HTML end tags (</p>, </div>, etc.), but it is one of the most important concepts in PHP. The semicolon indicates the end of a line in PHP and is absolutely intrinsic to your code- forget one and the script won’t work. The only time when you don’t need one is in the example above: comments- look back and notice that there are none there.

This entry was posted in PHP and web. Bookmark the permalink.

One Response to PHP Tutorial: Prerequisites and basic syntax

  1. useful info. thanks..soon html5 will take over the web :)

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>