Php tutorial Part 1 - introduction

Php is a very powerful programming language for building web based applications. But it is also easy for beginners to pick up.


Before you can start programming with php you need to do one of two things:

1. Get a web host that gives you access to php.
2. Install it on your computer.

To run php scripts on your computer you will have to have php installed, and also have a web server such as apache installed.

There are two ways to go about this:

1. Manually install and configure php and apache(or web server software of your choice) .

2. Go here and download an easy installation package. I would recommend against using any package that installs a version of php that is older than version 4.2 . Ideally you want a package that includes php version 4.3 or higher.


If you've never installed software manually by copying files to different directories and editing configuration files, and you aren't too comfortable with running programs from the command line, then I'd recommend you use method two and get an installation kit for php.

Either method you choose, you should be ok as long as you follow the instructions that come with the distribution.

Ok, now install the software and read the directions that come with the distributions that you choose on how to get the software up and running.

Once you have the webserver running you should be able to type this into your browser address bar(excluding the quotes): "http://localhost"

Ok, if you don't get an error page you are ready to write your first php script.

Open up a text editor of your choice. Notepad will do for now if that's all you got.
Copy and paste or type the following into it:

--- start copy below here ----

<?php
echo "Look at me, I'm a php programmer!";
?>

----- end copy above here ------

Save this file as "test_php.php" in your server's web root directory. The name of this directory depends on which package you install. It is usually either "htdocs", or "www".

Now go to "http://localhost/test_php.php" in your browser.

It should say "Look at me, I'm a php programmer!" in your browser.

Ok, now a line by line explanation of this very large three line program.

Line 1
Since php can be, and often is embeded into plain html, you need a way to tell the php parser what parts of your text file is php code that needs to be processed. You do that by using the <?php opening tag.

Line 2
The way you send stuff to the browser with php is by using the "echo" command. This line echos some text to the browser. Any text that you send to the browser with the "echo" command should be enclosed in single or double quotes. Both of these type of quotes should share the same key on your keyboard.

The php parser has to know where the text starts and ends, so you need to put it in quotes. And also since it uses these quotes to determine where your text begins and ends, you need to make sure that the type of quote you use doesn't actually appear in the text.

Example of the wrong way:

<?php
echo "Ed said, "I think php is cool".  I think so too.";
?>

This will cause an error. You have to choices. You can either put the entire text in single quotes('), or you can escape the quotes used in the text.

Examples of the right way:

<?php
echo "Ed said, \"I think php is cool\".  I think so too.";
?>

or 

<?php
echo 'Ed said, "I think php is cool".  I think so too.';
?>

When you put an escape character( \ ) in front of something it tells the program to treat it as regular text and not as having any special meaning to the program.

The other thing to point out is that you always need to put a semi-colon at the end of your echo statement. Without the semi-colon, you'll get an error.

Line 3
This closes php and goes back to html mode. Every opening php tag should have a corresponding closing tag.



You can freely go between html and php mode in the same file. Here's and example:

<html>
<body>
This is regular html.
<br>

<?php
echo 'This is php <br>';
?>

Back to html.
<br>

<?php
echo 'Back to php <br>';
?>

</body>
</html>

Now you've been introduced to some of the very basics of php.
In future tutorials I'll talk about ways to store values and how to make decisions in your program.
Next Part 2...  Creating html forms with php