PHP Introduction

PHP stands for is “Hypertext Pre-processor”. But that would make it HPP, surely? An alternative explanation is that the initials come from the earliest version of the program, which was called Personal Home Page Tools. At least you get the letters “PHP” in the right order!

PHP is probably the most popular scripting language on the web. It is used to enhance web pages. With PHP, you can do following things

  • Create username and password login pages
  • Check details from a form, create forums
  • Picture galleries
  • Surveys

and a whole lot more. If you’ve come across a web page that ends in PHP, then the author has written some programming code to liven up the plain, old HTML. PHP script will run inside the HTML file.

PHP is known as a server-sided language. That’s because the PHP doesn’t get executed on your computer, but on the computer you requested the page from. The results are then handed over to you, and displayed in your browser. Other scripting languages you may have heard of are ASP, Python and Perl.

But PHP is so popular that if you’re looking for a career in the web scripting industry then you just have to know it. In these tutorials I will teach you step by step PHP.

You’ll also need to have a server, to test your scripts. Don’t worry, though – we’ve found an easy way to get a server up and running on your own PC

 

Why learn PHP?

  • Easy to learn and understand
  • Processes data fast and dynamically
  • Free open source technology
  • Works very well with Mysql

 

Who is using PHP?

  • Facebook
  • Google
  • WordPress
  • Joomla
  • Zen Cart
  • Web Intersect
  • kaushalstreet

 

How to run PHP scripts on your PC

Before you can write and test your PHP scripts, there’s one thing you’ll need – a server. PHP is a server-sided scripting language, you either have to get some web space with a hosting company that supports PHP, or make your computer pretend that it has a server installed. This is because PHP is not run on your PC – it’s executed on the server. The results are then sent back to the client PC (your computer).

There is an easier way to get you up and running. We’re going to be using some software called Wampserver. This allows you to test your PHP scripts on your own computer. It installs everything you need, if you have a Windows PC. We’ll explain how to get it installed in a moment, and where to get it from. But just a word for non-windows users

 

Apple Users

If you have OS X, then try these sites to get up and running with PHP

  • http://www.onlamp.com/pub/a/mac/2001/12/07/apache.html
  • http://www.entropy.ch/software/macosx/php/

What you’re doing here is getting the apache server up and running, so that you can run PHP scripts offline. Pay particular attention to where files are stored, and to the “localhost” address.

 

Linux Users

There are quite a few sites out there to help Linux users get up and running with the Apache server and PHP. Here are three sites that are worth checking out:

  • http://en.wikipedia.org/wiki/LAMP_(software_bundle)
  • http://www.php-mysql-tutorial.com/wikis/php-tutorial/installing-php-and-mysql.aspx
  • http://www.phpfreaks.com/tutorials/12/0.php

 

Windows Users

OK, back to Wampserver and Windows. First, you need to download the software. You can get it from here (this site is nothing to do with ours, by the way):

Here is the link for download wamp server:

http://www.wampserver.com/en/#download-wrapper

 

How to use wamp server:

Hopefully, you have now downloaded and installed Wampserver. This will give you a server on your own PC (Windows users), somewhere you can test your scripts.

If the installation went well, you should have an new icon in the bottom right, where the clock is:

Click the icon to see the menu above

From the above menu you can do the following things:

  • Stop the server
  • Exit it
  • View help files
  • See the configuration pages

Click on localhost, though, and you’ll see this page appear: (Localhost just refers to the server running on your own computer. Another way to refer to your server is by using the IP address 127.0.0.1.)

Click on phpinfo() and you will see the below page:

How to save your PHP files:

You have to start the Wamp server every time you will create a new page and save file. Whenever you create a new PHP page, you need to save it in your WWW directory.

When you click on www directory, you should see an explorer window appear. This one is from Windows Vista: (You’ll probably have only two files, index and testmysql.)

Location of WWW folder: “c:/wamp/www/”

 

Working with PHP scripts

Suppose you have created a php script called test1.php. To launch this script, you need to add the script name after localhost in your browser. So instead of this:

 

http://localhost/index.php

You would type this: http://localhost/test1.php

You don’t type the name of the wamp folder, however. This would be wrong, for example:

c:/wamp/www/test1.php

OK, we’ll assume that everything is now up and running.

 

Variables in PHP

A variable is just a storage area. You put things into your storage areas (variables) so that you can use and manipulate them in your programmes. Things you’ll want to store are numbers and text.

If you’re ok with the idea of variables, then you can move on. If not, think of them like this. Suppose you want to catalogue your clothing collection. You enlist two people to help you, a man and a woman. These two people are going to be your storage areas. They are going to hold things for you, while you tally up what you own. The man and the woman, then, are variables.

You count how many coats you have, and then give these to the man. You count how many shoes you have, and give these to the woman. Unfortunately, you have a bad memory. The question is, which one of your people (variables) holds the coats and which one holds the shoes? To help you remember, you can give your people names! You could call them something like this:

mr_coats

mrs_shoes

But it’s entirely up to you what names you give your people (variables). If you like, they could be called this:

man_coats

woman_shoes

OK, so your people (variables) now have name. But it’s no good just giving them a name. They are going to be doing some work for you, so you need to tell them what they will be doing.

 

Putting value into variables

The man is going to be holding the coats. But we can specify how many coats he will be holding. If you have ten coats to give him, then you do the “telling” like this:

mr_coats = 10

So, the variable name comes first, then an equals sign. After the equals sign, you tell your variable what it will be doing. Holding the number 10, in our case

However, you’re learning PHP, so there’s something missing. Two things, actually. First, your people (variables) need a dollar sign at the beginning (people are like that). So it would be this:

$mr_coats = 10

$mrs_shoes = 25;

 

So, $mrs_shoes is holding a value of 25. If we then wanted to add up how many items of clothes we have so far, we could set up a new variable (Note the dollar sign at the begining of the new variable):

$total_clothes

We can then add up the coats and the shoes. You add up in PHP like this:

$total_clothes = $mr_coats + $mrs_shoes;

Remember, $mr_coats is holding a value of 10, and $mrs_shoes is holding a value of 25. If you use a plus sign, PHP thinks you want to add up. So it will work out the total for you. The answer will then get stored in our new variable, the one we’ve called $total_clothes. You can also add up like this:

$total_clothes = 10 + 35;

Again, PHP will see the plus sign and add the two together for you. Of course, you can add up more than two items:

$total_clothes = 10 + 35 + 7 + 38 + 1250;

But the idea is the same – PHP will see plus signs and then add things up. The answer is then stored in your variable name, the one to the left of the equals sign.

In the next part, we’ll take a look at how to put text into variables.

Putting Text into Variables

In the previous section, you saw how to put numbers into variables. But you can also put text into your variables. Suppose you want to know something about the coats you own. Are they Winter coats? Jackets? Summer coats? You decide to catalogue this, as well. You can put direct text into your variables. You do it in a similar way to storing numbers:

$coats1 = “Winter Coats”;

Again, our variable name starts with a dollar sign ($). We’ve then given it the name coats1. The equals sign follows the variable name. After the equals sign, however, we have direct text – Winter Coats. But notice the double quotation marks around our text. If you don’t surround your direct text with quotation marks, then you’ll get errors. You can, however, use single quotes instead of double quotes. So you can do this:

$coats1 = ‘Winter Coats’;

But you can’t do this:

$coats1 = ‘Winter Coats”;

The direct text will then get stored in the variable to the left of the equals sign.

So, to recap, variables are storage areas. You use these storage areas to manipulate things like text and numbers. You’ll be using variables a lot in PHP sites.

 

Some practice with variables

First, we’ll take a look at how to display what’s in your variables. We’re going to be viewing our results on a web page. So see if you can get this script working first, because it’s the one we’ll be building on. Using a text editor like Notepad, or your PHP software, type the following. (You can copy and paste it, if you prefer. But you learn more by typing it out yourself – it doesn’t really sink in unless you’re making mistakes!)

When you’ve finished typing it all, save the page as vpratice.php. Then Run the script. Remember: when you’re saving your work, save it to the WWW folder.

To run the page, start your browser up and type this in the address bar:

http://127.0.0.1/variables.php

Or this:

http://localhost/variables.php

If you’ve created a folder inside the www folder, then the address to type in your browser would be something like:

http://127.0.0.1/FolderName/variables.php

Add the following lines in your code:

We’ve set up a variable called $test_String. After the equals sign, the text “It Worked!” has been added. The line is then ended with a semi-colon. Don’t run your script yet. Change the Print line to this:

  • print($test_String);