What is PHP?

"Simple is Hard" PHP talk by Rasmus Lerdorf at National Taiwan University

You can already sponsor this chapter!

PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document. It also has evolved to include a command-line interface capability and can be used in standalone graphical applications.

PHP was originally created by Rasmus Lerdorf in 1995. The main implementation of PHP is now produced by The PHP Group and serves as the de facto standard for PHP as there is no formal specification. PHP is free software released under the PHP License which is incompatible with the GNU General Public License due to restrictions on the usage of the term PHP.

While PHP originally stood for "Personal Home Page", it is now said to stand for "PHP: Hypertext Preprocessor", a recursive acronym. (Read more on Wikipedia).

The last stable version of PHP ist 5.3.8 which was published in August 2011.

Joomla! is written in PHP. Thousands of files contain PHP code which was written over the last years. Joomla! consists of more than 500.000 lines of PHP code. It would need 244 person-years to develop it! (Joomla! estimated cost)

When Joomla! was founded the developers used mainly PHP 4.x which was very common these days. It was and partly is a challenge to rewrite the legacy code to use as many as possible features of PHP 5.x


Let's have short overview of PHP. If you ever had a programming course in school you will remember most of the statements. If you are totally new, just have a look and try to understand the code. The example files are attached at the end of this page. It is a very good idea to try the examples on your own machine.

Where is my PHP?

If you use a LAMP bundle, PHP is inbuild. Usually it is a binary file, tied to the Apache Webserver as a module. When you start the Webserver, PHP is ready to run. PHP has feature called phpInfo. It showes the configuration of everything which is related to your PHP interpreter. In MAMP, you can click on phpInfo to see that page (Figure 1).

cocoate.com/node/9584PHP Info - MAMP

Figure 1: phpinfo via MAMP

It is very easy to produce the same output on your own. Just create an empty file with the name phpinfo.php (the name doesn't matter, could be also joomlarocks.php) in your editor and type in this code (Listing 1).



Listing 1: phpinfo.php

Place the file in the /htdocs folder access it via http://localhost/ and click on the filename (Figure 2).


Figure 2: Webserver directory


Depending on the the LAMP bundle you are using

  • the domain localhost is tied to various ports. The default setting in MAMP e.g. is port 8888 and you have to write http://localhost:8888. Check your port in the documentation. If it is port 80 it it is the default port of "the internet" and it is not necessary to write it. http://localhost:80 is the same then http://localhost
  • Usually you see a directory when accessing http://localhost. This is a configuration of your Apache webserver. If you don't see a directory, create an additional folder in /htdocs e.g. php and access it via http://localhost/php. If you still do not see a directory access the file directly via http://localhost/php/phpinfo.php and search for a solution in the documentation of your LAMP bundle.

As you see, PHP programming starts very simple :) Any PHP script is built out of a series of statements.

Hello World

If you haven't done it so far, please create a folder called php in the htdocs folder of your server. Let's start with the hello world example (Listing 2)



Listing 2: hello.php

The PHP interpreter only executes PHP code within its delimiters. Anything outside the delimiters is not processed by PHP. Delimiters are configurable but the most common delimiters are <?php to open and ?> to close PHP sections. If you think of creting PHP codes for a website a more realistic example would be something like listing 3. In this listing you can see the typical mixture of HTML (HTML5) and PHP.

Listing 3: hello_html5.php

PHP is not complicated, the biggest problem is to figure out the right syntax and the concepts in general.


A variable is a symbolic name for a piece of data. The idea behind is to have a name or a kind of a 'pointer' for this data to be able to use it in a script. The data of the variable may change in one script (Listing 4).



Listing 4: variable.php


PHP has a lot of inbuild functions like print() or phpinfo(). But the real power comes with self made functions that are tailored to your needs. In listing 5 you see an example of a self made function. In your browser the result will be this sentence The date is 2011-11-02.



Listing 5: function.php


It is possible to use parameters in functions and of course in several other places. In the example in Listing 6 I use two parameters. The first parameter is the format of the date ($format) and the second parameter is the punctuation ($punctuation). Parameters can be used as variables in functions.


'.$format.' the display will be ';  
  echo ''.date($format).'' . $punctuation.'

// this is the main script
echo 'If you write something like: 
writedate('l jS \of F Y h:i:s A','.');


Listing 6: parameter.php

In your browser it will look like:

If you write something like:
- Y-m-d the display will be 2011-11-02,
- H:i:s the display will be 18:32:33,
- m.d.y the display will be 11.02.11.
- l jS \of F Y h:i:s A the display will be Wednesday 2nd of November 2011 06:32:33 PM.

Return Values

Sometimes you want to outsource some code to a different place. For example a calculation. One possibility is to use a function. The code of the function is always the same but the return value depends on the given parameter.



Control Structures

PHP provides the usual suspects:

If Else

If an expression is true like $a >$b execute a statement. If not ... else ... than execute another statement.



 $b) {
  echo "a is greater than b";
} else {
  echo "a is NOT greater than b";


If Elseif

In this construct it is possible to ask twice if ... elseif ...else.



 $b) {
    echo "a is bigger than b";
} elseif ($a == $b) {
    echo "a is equal to b";
} else {
    echo "a is smaller than b";



The while loop executes the statement as long as the while expression is TRUE.





Foreach iterates over arrays and only works with arrays. An array is a list of values.




The switch statement is similar to a series of if statements on the same expression. If you want to compare the same variable (or expression) with many different values, the switch statement is more elegant than a number of if statements.





The main difference between PHP4 and PHP5 was the rewritten object model. See chapter What is Object Oriented Programming for more information on this topic. A basic example would look like Listing 7.


colour = $colour;
       $this->brand  = $brand;
       $this->image  = $image;
   public function startEngineMethod() {
       return ' The ' . $this->colour . " " . $this->brand . " starts its engine.";
$her_car = new Car('red', 'Ferrari', 'http://farm4.static.flickr.com/3004/2541945935_422339cbef_t.jpg'); //Photo by exfordy (CC BY 2.0) 
$his_car = new Car('blue', 'Smart', 'http://farm1.static.flickr.com/66/222092351_c9b93d3286_t_d.jpg'); // Photo by cocoate  (CC BY 2.0)
$other_car= new Car('','Volkswagen',  'http://farm4.static.flickr.com/3040/2746837856_7acb6535c0_t_d.jpg'); // Photo by Glen Edelson  (CC BY 2.0)
echo $her_car->startEngineMethod(); // prints "The red Ferrari starts its engine."
echo '

'; echo $his_car->startEngineMethod(); // prints "The green Triumph starts its engine." echo '

'; echo $other_car->startEngineMethod(); // prints "The Volkswagen starts its engine." ?>


Listing 7: class.php

The result in the browser will look like in figure 3

cocoate.com/node/9584Class Example

Figure 3: Output of class.php

By far the best reference for PHP is the php.net. If you are curious play around and try out as much as you can.

php_listings.zip <- unzip first4.57 KB