The php5 object oriented web design howto - What is php?

php is a server side scripting language, and if you don't know what that means... don't panic this page will show how to use it.

Since it is a scripting language you place your code in a normal text file and then name that file with .php at the end. Then you move this text file into the file tree of the webserver, with scp, ftp or a webinterface. If you don't know contact your webserver administrator and ask him.

Hello world

And how do one of those files look like then? Let's begin a rock bottom with a Hello world example.

Filename: p01_ex01.php

print "Hello world...\n";

First we have a start php tag, that tells the server that php code will follow, <?php.

Then we call the print function that shows "Hello world" on the page. print "Hello world...\n";.

And finally we end the php code with a end tag that looks like ?>.

A variable

Then it is time to enhance the program with a variable, and a variable is just a place where you store away stuff that you will use a little bit later.

Filename: p01_ex02.php

$tmp = "Hello world again.\n";
print $tmp;

Here we stored a text string in a variable called $tmp, and then one line later we print this string.


Then sometimes you will find a need to comment something in your code, and to do this in a way that will not show.

Filename: p01_ex03.php

// This is a one line comment 

 * This is a multi line 
 * comment that also does nothing.

But since the browsers only understand html (it is only the server that can see the php code), you must make sure that you have proper html tags around the text. This is called to print data with html syntax.

Filename: p01_ex04.php

print "<h1>A header</h1>\n";
print "<p>some text</p>\n";

Mix php and html

Then we have a last thing that is good to know about basic php, and that is that you don't have to write everything in php. Sometimes it is a good idea to stop the php script and write some static html, and then continue the php script a little bit later.

Filename: p01_ex05.php

print "<html><body>\n";

<p>Some normal html code</p>

print "</body></html>\n";