I learned this web programming language by myself and found some good online tutorials (below), yet, once I did learn the language and had a better understanding of it, I felt I could give a better and more interesting introduction to the language.
This introductory tutorial to PHP is designed for absolute beginners in programming and gives you a good overview and starting point. It is an easy language to learn and use, and almost anyone should be able to program in it.
First of all, one advantage of PHP is that it is frequently used, it is an easy language to understand, and most importantly, it is relatively easy to find scripts on the net which you can download for free and use for your own purposes. In this sense it is an open source software which many programmers contribute to.
For example, let's say you want to add to your page a clock with an alarm. You could run a search on google for:
php script clock alarm
and you will find many results. It may take you a bit of time to filter out all the hype and hone in on a script with the functionality and appearance you like, but this will take you much less time than to build the script yourself from bottom up. Furthermore, you can read and study the script to give you an idea how the programmer developed it, and learn more PHP in the process. Studying the programmer's approach is voluntary, whereby technically you could have a fairly weak knowledge of PHP yet still be able to incorporate different scripts into your webpages.
To accomplish this you first must convert your html page into PHP format.
If you are a beginner in HTML, you can use something simple like FrontPage to type text, insert a picture and do other basic formatting like you would in Word. After visually preparing your page, you can then view its HTML code to understand that language and what the particular codes or scripts are to insert pictures, change background colour, font size and so on.
Once you have completed your html page, you can now convert it to php
format. Change the file extension from .html to .php and open up the file in Notepad or
some appropriate software, such as Dreamweaver, or my preferred –
HTML Kit. This is free software with good support, opens fast, and helps you organize the script in different colours (which you can also change yourself). As opposed to viewing the file in Notepad, which can seem like a painful goulash, programs such as HTML Kit automatically colour certain sections to help you understand the logic flow.
But before you start working on your new php page, you must first be able to view it properly. In the previous html format you would normally just double click on the file and it should be correctly viewable in the default browser installed on your computer. But php language is dynamic, for which reason you need to create a functional web-like environment on your computer.
The basic and hard way to accomplish this is to go to php's website and download its free installation software. Or they might link to some third party packages. But generally I find php's website rather difficult to understand.
I studied some popular packages and eventually decided on Apache2Triad. It installs php, perl, mysql, and all sorts of goodies. But it is quite robust and can take up a fair amount of your system's resources,
for which reason I like to use PHP Portable (which can also run sqlite databases), which is so light and small it can run on a flash card or CD/DVD. This is useful if you'd like to take your fancy php creations and show them on other computers without having to install anything on them.
* Note though that, once you do have your php web environment installed and set up on your computer, to properly show the php pages in a browser you cannot just double click on the files but you must open them with the proper address in the browser strip (for example, http:// … /site.php in your browser – not c:\PHP_Portable\files\site.php, which would appear if you simply double clicked on the file in your computer).
Now that you have your new php file correctly visible in your browser, and with the script code visible in your HTML Kit program, you can start to make changes to the script to see what happens to it in the browser.
For example, you make a change to the php page in HTML Kit, save the file (ctrl s), do the famous ALT TAB to jump to your browser and press F5 (Refresh) to see the updated changes.
Before we start making any changes, we should understand the slight differences between html and php.
First of all, any special php script within the page must start with a
and end with a
For example, you have some regular html script, explaining where a picture goes or what the text looks like, and now you want to insert your alarm clock. So your page might look like:
…bunch of html text…
…fancy php script about the clock which you found and downloaded from the internet
…bunch of html text…
Or, for example, there is no reason why your page cannot start with <?php and end with ?>, making your entire page php. You can weave in and out of the two languages, back and forth as many times as you like, each time describing to the browser which language starts and ends.
But if you do want to use html language WITHIN your php sections, you have to convert them a bit. For example:
<tr><td colspan='5'><img src="/images/spacer.gif" width="100%" height="1" style="background-color: #BBBBBB;"></td></tr>
echo "<tr><td colspan='5'><img src='/images/spacer.gif' width='100%' height='1' style='background-color: #BBBBBB;'></td></tr>";
The big difference is that all double quotes " within the html language should be converted to a single quote ' (you can select a certain section and just search&replace), and then surrounded by
echo " …converted html script… ";
echo in php speak is basically the same as a command "print" and just prints something onto a page.
For example, you can create a file called page.php in notepad and have only the following contents within it:
Such a simple page would only show the word "howdy" at the very beginning and nothing else. The php command "echo" just says to print onto the webpage whatever is found between the double quotation marks.
After howdy you could add <br><br> to produce two lines (standard html code), and then copy in any php script you find on the net, instantly giving your page the functionality you desire.
Inevitably you will run into glitches, but by finding what you did wrong you will learn in the process.
For example, it is important that every line in a php file ends with the ; character. If not, the browser will think the next line is a continuation (not separate command) and get confused. Therefore, when copying in your script snippet make sure it starts and ends correctly, and that there is no extra or lacking ; character.
One of the important concepts is to understand that a computer thinks logically and starts reading your script from the beginning of your page, simply following your instructions until the end.
For example, your page might follow the following logic:
- show a certain picture
- show certain text with defined formatting
- show a button which the user can press
- if the user presses the button the first picture changes to something else
- if the user does not press the button, do nothing
Understand that you can stick IF ELSE statements in your page to add functionality to it.
GO TO statements are also useful.
For example, you can have a page:
- start a particular process that does 26 different tasks, from A to Z
- show a button
- if user presses button, Go To task P
By pressing the button, the user is essentially stopping the process however far it has gone so far (let's say up to the letter E), immediately jumping to process P and skipping everything between E and P.
In this way you can understand that the computer thinks very logically and you can use IF ELSE and GO TO statements to create a very complex logical loop. You have to understand the logical flow and pay attention to it very meticulously. For example, if your logic jumps through some loops and eventually gives the command to divide 1 by 0, your browser will display an error result (like any calculator), because this is a mathematical impossibility. Your logic has to make good sense, without glitches, and it cannot have any syntax errors (such as forgetting the ; character after every line). If your script does run into glitches, you must use your logic to find out on which line you made a mistake. It is a great way to learn the language, and often you will smack your forehead with your palm once you realize what a silly mistake you had made. Computers may seem very smart but, in fact, they only follow your instructions and seize when you instruct them something which does not logically work. They do not yet contain intelligence to study your script while you develop it and point out problems or assume what you meant.
Functions – the purpose of a function is to define a task or piece of script so you can call it up any time without the need to write out the script every time.
For example, you may have a function called Separate which may have 15 lines of complex script and another function called Join which has 10 lines of script with instructions. For security and efficiency purposes you might often embed and define your functions in a particular file, such as config.php.
Let's say you are working on a file called 22.php and you want to use one of these functions. At the top of 22.php you would include the line:
which basically instructs the browser (computer) at the start of the file that it should also consider this config.php file while processing 22.php. You can include or require_once as many files as you like.
Therefore, let's say half way down 22.php the computer runs into the following script:
if user presses button1, run function Join
else if user presses button2, run function Separate
else continue to next point
I'm obviously not using correct PHP language or syntax but just writing out the flow of logic, because I think you should understand these basic concepts before diving into the other tutorials below.
In this way you can call up any function defined in any files without having to always write out the same 15 or 10 lines etc of script. Saves a lot of writing or copy/pasting and also speeds up your scripts because the browser does not have to constantly download the same repeated scripts.
Variables – Variables are very useful like functions in that they allow you to define certain concepts and again save on a lot of repetitions.
To define a variable you invent a name you like and put a
$ (string) in front of it.
For example, you might have
$a = "lets go for a beer";
$b = "lets watch a movie";
$mood = "neutral";
You've just defined mood as neutral. But lets say
somewhere else you have the logic:
if Person met ex-girlfriend
$mood = "upset";
else if Person met potential_girlfriend
$mood = "romantic";
Basically this script defines the variable 'mood' based on certain circumstances: if neither of the ifs are met, mood does not get redefined and remains as 'neutral'.
If you have not redefined this variable mood, you can work through a hundred pages of script to eventually stumble upon:
echo = "Boy says $a:;
else if $mood=romantic
echo = "Boy says $b";
else person keeps walking
In this way you can use functions and variables to help reduce the size of your script and not have to constantly repeat things, combining it with IF ELSE and GO TO statements to create complex functionalities and possibilities.
You can do some neat stuff in PHP alone, such as our alarm clock and all sorts of other things, but PHP can become even more powerful if combined with a database. The most common form of database which combines with a php script is MySQL. But MySQL can be more difficult to set up and require a greater learning curve, which is why I preferred to start with SQLite. But not all servers such as A2 support this form of database. You would need to check. In any case, if your server webhost does support sqlite, I find it a lot easier to work with (although the language is quite similar to mysql).
To understand how a database works, imagine you have a table called countries (the top row always shows the field names):
The point of a database is to organize data into concise and logical units in order to limit its size and help it run faster.
For example, lets say we have another table called Person:
In this way you can create a script which might say something like:
$door = "107";
if knock on $door=107
$sql = "SELECT Country_from FROM Person WHERE Person=$person";
$sql = "SELECT GDP FROM Countries WHERE Country=$Country";
echo = "The GDP of the country of the person, named $person, living behind door number $door is $GDP.";
The results of this script would be:
The GDP of the country of the person, named Bob, living behind door number 107 is 50.5.
In the $sql= section you can see the conditions it states when drawing data from each of the tables. For example, drawing from the table Countries the GDP of the country of the person associated with door number 107.
You should now begin to perceive how the php script draws data from a database (composed of many tables) to define variables and produce results. The reason why you want to break up your data into several tables is to avoid duplication. If, for example, you have 10,000 people in your database, where one third of them are from Canada, instead of repeating the details of Canada for each of their records (which would make the People table very large and full of duplicated data), you create a new table for Countries and only refer to the country reference* when accessing details of that country. This avoidance of duplication greatly speeds up your database and webpages.
In your sql statements, you instructed the computer from which table to draw your data and defined a new variable based on the results. This defined variable you then used to extract from another table. This is the basic logic how any programming language draws data from tables within a database.
This same database could be used in a webpage, where you could have something like:
echo = "The GDP of $country is $GDP";
where $country is defined by a dropdown list (such as: ) which the user can click on and choose a particular country. The countries in the list can be drawn from a table and hence do not have to be typed out every time [such as: Create list BASED ON field Countries IN Countries table].
The data in the table can also be changed. For example, there might be a special php page named alter_GDP.php which is located in a special password protected folder. The Administrator can gain access to this file because they know the username and password to enter the folder. Once in the php file they can then change the GDP information of any country in the table, which will immediately update online. Or perhaps the GDP could be linked to another website where this information is updated every week. As soon as the country updates this information on its website, the php script will recognize the change and automatically update its own table data to reflect the new details.
In this way the php script can use functions, variables and various commands in combination with data in a database to create complex and dynamic webpages. You can check out a demo version < for one database project I set up to get an idea of how php can work together with a database.
You thought the fun stopped here? It's technically just the beginning!
Think of PHP as particular commands and functions which produce a particular result. For example, a webpage which functions as a calculator. You have a small box where to put your first number, then a drop down list of operators (times, add, divide, subtract etc), then a second blank box to type in your second number, and at the end a Compute button which executes the calculation based on the two numbers you typed in and the operator you chose between them (such as 2x2=4 – the result produced being 4).
The problem is that you first have to define all your variables (first and second numbers, and the operator), and then press the Compute button to get your result.
Below are two examples of simple php pages which are incorporated into this page through iFrames (so that this entire page does not have to reload every time the Submit button is pressed or a choice is made).
Press the link below each of the frames to view the script and understand the general logic.
used for this page as well
to produce a nice popup window for the above two links.
A recent language pushed most by Google, this language can be a bit difficult to understand, but quite useful if you want to make faster operating webpages.
For example, lets say you are filling in an application form, and the page asks you what country and city you are from.
For example, you are presented with a dropdown list of several
continents. As soon as you choose your continent (let's say
You can now see how the combination of these various languages/systems and databases are used to create a very functional and useful website.
The advantage of using such scripts in the first place is that you can create a useful website which requires less maintenance.
For example, you post the above table of countries with their GDPs. You receive an email notification from some service in some country informing you that the GDP has changed, so you go to your static html page, manually make the change, and upload it to your server.
But with more complex sites you could be spending 8 hours a day maintaining and making changes to it. The purpose of dynamic webpages is to provide some useful service which does not require much or any maintenance. People can log in, add comments concerning the GDP of each country, interact with one another, receive updated information automatically, with absolutely no maintenance or work required on your part at all!! That is the beauty of the internet, and now all you have to do is think up an interesting concept or service and formulate it in some functional programming language. And if you're creative and forward thinking enough, you can come up with some concept like Facebook and eventually become a billionaire!
Hopefully this has given you a rough overview of these various languages. Keep in mind that there are many programming languages out there, such as .asp, flash, perl* and so on, where many of them can be used together to create complex and very useful webpages. Good luck and happy creating!
This is different from embedding php script within a php page because the page which is embedded into another page through/within an iFrame must be an entirely functional and self-sufficient webpage, whereas php script embedded or copy/pasted into a php page can be an individual function or task etc. which operates within and is incorporated as part of that php page. Such as a complex computation algorithm which makes a calculation when a particular button on that page is pressed.
* Perl – In many ways, Perl is similar to PHP, it was created almost a decade before PHP, and also has a significant following and support. Perl scripts often end with a .cgi extension and their functionality can be quite complex. But generally it is considered a more cryptic language with much variability and potential confusion. PHP is more standardized and simplified, so a much better starting point. You can always take on perl in the future. The language, in fact, is very similar to PHP and it is easy to follow its written logic.
Now that you've read this page you should have a rough overview of what PHP is and how it works, and you should be ready to start reading some tutorials and program your first pages. Here is one PHP Tutorial I found easy to read and fun, and W3Schools has a lot of useful tutorials. Or you can just google "PHP Tutorial" and other similar searches.
You might also be interested in reading my own PHP programming notes, which I compiled over time, although the notes are somewhat abbreviated and might not be the easiest to understand. In any case you could use it for your further reading as there are many useful links within it. Have fun!
Copyright © KENAX, by Karel Kosman - All Rights Reserved Worldwide.