Getting started with PHP: Installation, IDE and Framework

This tutorial is geared to someone who whishes to start developing in PHP. We will introduce the necessary tools to host, develop and debug a PHP application.

This is not a programming tutorial, although at the end we give a few pointers to PHP beginners. Don't expect me to explain algorithmic logic or what's a cycle. I expect the reader to already be acquainted with programming software.

For someone who is used to another developing environment, like .NET or Java, they expect one unified solution that includes the necessary core-framework of the language, IDE and general framework (e.g. someone who develops in .NET usually uses Visual Studio that integrates all these things).

This kind of solution doesn't exist in PHP, because there are so many choices that could be made and that can throw new users off (especially for someone who comes from another background) and that's why this tutorial is needed.

First, we start with the necessary software needed to run PHP applications. Then we go over our IDE of choice. Finally we present a few possibilities for the framework and our best pick (this is a more sensible subject). All these tools are free and cross-platform, of course.

At the end, a crash course on PHP is provided, to introduce some of its distinct characteristics.

The AMP acronym: Apache, MySQL and PHP

That's what you need for running PHP capable applications. The pages are served through the Apache HTTP Server, interpreted by PHP, with MySQL used for the database.

I know what you are thinking: so many things to install and configure - what a pain! Because this is a very used combination there are already many packages that include all of this and are a breeze to install - these are usually called WAMP (Windows, Apache, MySQL and PHP/Perl/Python) or LAMP (Linux, Apache, MySQL and PHP/Perl/Python).

There's an exception: XAMPP! The X meaning that the OS (Operating System) could be any of the following: Windows, Linux, Sun Solaris or Mac OS X. The two Ps are for PHP and Perl.

I recommend installing the XAMPP package as I haven't any complaints with it. The installation is easy, management is user-friendly (mostly done through the browser) and there's even a portable version of XAMPP. Don't forget to follow the security recommendations after you install it - they are on the website's manual for each different OS. XAMPP also comes with other important modules like phpMyAdmin, which provides the ability to handle administration of MySQL in the browser (pain-saver sometimes).

Alternatively you could use another AMP package or you could even test it directly on a remote host, but if you want to run your applications locally then you obviously need AMP installed (and, of course, this provides for a much better development experience and is recommended).

This is a picture of the XAMPP's Control Panel running on Windows Vista, providing real-time status of its different modules.

The best free IDE for PHP

When it comes to the IDE (Integrated Development Environment) there is a lot of choices. There are some people who like to code using only a simple text editor with syntax highlighting, but that's not really what an IDE is (though I also use these sometimes, especially for the quick fix). An IDE is much more than that - it includes tools to help you be more productive: debugging tools, code assist, code folding, project management, etc.

There are a lot of different IDEs out there and the choice was difficult in the past. The IDEs were usually divided in two categories: those that were free, but lacked the necessary advanced tools, and those that were paid, with more advanced features, but who wants to spend any money, uh? ;)

Now the choice is quite easy, because Eclipse PDT has arrived. It's an Eclipse project specifically targeted to PHP development. It's free and includes many advanced features that were not found in other free IDEs for PHP at the time, like debugging and code assist (not just for standard functions - very important, you don't want to miss this!). I think this could become the standard IDE for PHP in the future if it continues to be well developed.

You should read this nice article on setting up Eclipse PDT: Getting Started with Eclipse PHP Development Tools (PDT). It will guide you through installing the IDE and add some extra features like the Zend Debugger, JSEclipse for improved JavaScript features and Subclipse for Subversion revision management.

I use Eclipse PDT but there are other IDEs you may want to look at:

  • Free
  • Paid
    • Zend Studio: maybe the best PHP IDE there is; the cheapest version is the Standard edition ($99 as of writing).
    • phpDesigner: nice editor with good features; cheapest version is the Academic License ($45 as of writing).
    • Rapid PHP: cheap editor with nice features ($29.85 as of writing, for non-commercial use).

Choosing the right PHP framework

Oh boy, this is when things get ugly. If you do some research you'll see there is a lot of discussion about PHP frameworks. This is because there are a lot of them, each one serves a different purpose and each has its share of passionate users.

Let's start by defining what is framework. A framework is a set of libraries and classes which support the base of an application. It provides the common elements that are usually present in any application (e.g. functionality for database interaction). This way you can spend more time focused on coding the application itself and this helps you be more productive. Examples of frameworks for other languages include .NET Framework and Struts for Java.

Why all the fuzz? A few years ago, PHP frameworks were scarce and generally went unnoticed. Programmers usually developed their own code and there's still a lot of them who work this way. But with more complex web applications and an increasing need to develop applications faster and more efficiently a lot of PHP frameworks started to surface. The PHP community is divided among many different frameworks and there is even those that use different ones simultaneous. So choosing the best PHP framework is a controversial and difficult job.

The bottom line is: there's no best PHP framework. You have to choose the one that best fits your needs and the one that you like working with the most. Having said that, I will give my suggestion on what framework you could choose and I will describe some different frameworks and their differences and similarities.

Zend Framework - Best pick

Zend Framework
Zend Framework

Why do I recommend the Zend Framework? For starters, it was made by the same people that made PHP and they offer a wide range of solutions for PHP development, so they naturally know what they are doing.

One of its main advantages is that it has the backup of many big names of the industry and Zend has made a good job marketing its framework. Nevertheless, the Zend Framework received a lot of criticism when it was released and with its initial versions, but I feel that most of those issues have been solved by now.

Secondly, it has everything in its favor:

  • Good documentation and tutorials
  • Easy installation
  • Excellent support for web services
  • Good localization features
  • Liberty to choose the architecture of your application

The last is particularly important, because there are a lot of frameworks that tie you to its own architecture (namely Model-view-controller), which can be a good thing also, but sometimes you need more flexibility. The Zend Framework is flexible enough to allow YOU to choose how you structure your application. It's very scalable for very small and also for very big projects. This also means that you should have the necessary knowledge to know how you should structure your applications, which can be bad for newcomers who may feel a little lost.

The Zend Framework also has great support and hundreds of contributors, which guarantee its successful continuation. It's a framework that's very easy to integrate with others and you can easily only use the parts of it that matter to you. It's not yet very AJAX-enabled (though you can easily integrate an AJAX library of your choice), but one of the main features of the next version is a killer AJAX support.

One of its great features is its excellent web services support and also the integration with a lot of popular web services. It's continually updated and that's also very important.

Although this is the framework that I use it doesn't mean it's the best - there are a lot of other good frameworks. I'll describe some of these other frameworks.

CodeIgniter - Simplicity is king

CodeIgniter
CodeIgniter

CodeIgniter is a lightweight PHP framework with simplicity in mind. It's very good for beginners because you can start producing simple websites very quickly. It's more geared toward producing small to medium sites, but it excels in this field because of its simplicity.

It's also easy to install, has good documentation, but it makes you follow the MVC architecture, which can be good because it already gives you a nice foundation for your site, but is less flexible than Zend Framework in this regard.

CakePHP - The sweetness of cake

CakePHP
CakePHP

CakePHP is a very popular PHP Framework. It has many strong points going for it. Certainly has many features, everything you will probably need.

Also easy to install and the documentation is good, but could be better organized. It's another MVC framework that requires you to follow this pattern.

One of its main characteristics is its ability to provide simplicity, but at the cost of knowledge (it's hard to know what's under the hood).

PRADO - The power of events

PRADO
PRADO

PRADO is sometimes called ".NET for PHP". It's an event-driven and component based framework, very similar to how .NET works. This event-driven paradigm is actually very nice to work with. It has its own templating language, also inspired by .NET.

It's a very unique framework and can make any .NET developer feel right at home.

symfony - Complex orchestra

symfony
symfony

symfony is a very big PHP framework with a lot of features. It's also complex and that's its main drawback, because it's hard to learn. Because of its size and complexity it's also slower than most top frameworks.

That shouldn't scare you, because it's also very good with a very big community behind it. It also follows the MVC pattern and it has good documentation. Features code generators and can handle very big projects.

Another good point in its favor is that there are a lot of companies that use it and it has dedicated sites for symfony-related jobs which help you find work.

An unattractive aspect of this framework is that it relies on the command line on several occasions throughout a project.

PHP crash course - What you need to know

This will be a very brief crash course on PHP. If you come from a C-like language background (C++, Java, C#, JavaScript, Perl, etc.) then you will feel at home with PHP (also based on C).

A few quick tips:

  • Instructions end with ;
  • PHP code must be place within special delimiters, the most common being <?php and ?>, first and last respectively
  • Comments are placed within /* */, or preceded by //
  • Variables are prefixed with the $ (dollar) sign, e.g. $variable = 3;
  • Variables are case-sensitive, but not function or class names
  • Strings can be defined with " (double quotes) or ' (single quotes)
  • Use the echo function for outputting, e.g. echo $variable;
  • Concatenate strings with the . (dot) operator, e.g. echo "This is a " . "test.";
  • Most C functions have a direct equivalent, e.g. echo sqrt(2);

PHP is an object-oriented programming language (not in the first versions, though). A few tips with objects:

  • Object members are called with the -> operator, e.g. $obj->a = 3;
  • Use $this inside a class definition when you want to access its context, e.g. $this->a = 3;
  • Static methods can be called with the :: operator, e.g. ClassName::staticMethod();
  • Constructors are defined with __construct()

A simple Hello World written in PHP:

<?php
 
    echo "Hello World";
 
?>

Variables in PHP don't have a type. This is valid code:

<?php
 
    // notice no variable declaration
    $var = "anything";
    $var = 3; // valid code
 
?>

This also allows you to compare numbers in string with integers, for example:

<?php
 
    $varOne = "1"; // a string with 1
    $varTwo = 1;
 
    if ($varOne == $varTwo) { // this is true
       echo "Both are equal";
    }
 
?>

If you want to test if the value is equal and of the same type use the === operator. Using the previous example:

<?php
 
    $varOne = "1"; // a string with 1
    $varTwo = 1;
 
    if ($varOne === $varTwo) { // this is false! Notice the === operator
       echo "Both are equal";
    }
 
?>

Arrays in PHP are very special, they don't need to be initialized. Example usage of arrays:

<?php
 
    /* all valid code */
 
    $arr[0] = 1;
    $arr[97] = "something";
    $arr["test"] = 123;
    $arr = array(1, 2, 3);
    $arr[] = 4; // array's values: [0] = 1, [1] = 2, [2] = 3, [3] = 4
 
    $arr = array("name" => "John", "age" => 20, "location" => "United States");
 
    echo $arr["name"]; // outputs "John"
 
?>

PHP also supports the foreach syntax. See the following examples:

<?php
 
    $arr = array("name" => "John", "age" => 20, "location" => "United States");
 
    foreach ($arr as $val) {
       echo "$val<br />";
    }
    // Outputs:
    //   John
    //   20
    //   United States
 
    // if you also want to get the key
    foreach ($arr as $key => $val) {
       echo "$key = $val<br />";
    }
    // Outputs:
    //   name = John
    //   age = 20
    //   location = United States
 
?>

If you want to access the GET and POST values, you can use the public $_GET and $_POST arrays. In your HTML file:

<form method="GET" action="page.php">
    <input type="text" name="username" />
    <input type="submit" />
</form>

In the PHP file (page.php):

<?php
 
    if (isset($_GET["username"])) {
       echo "Your username is " . $_GET["username"] . ".";
    } else {
       echo "You failed to provide a username!";
    }
 
?>

In PHP you don't need delegates or function pointers. The equivalent in PHP is:

<?php
 
    function test() {
       echo "test!";
    }
    $testing = "test"; // we define the name of the function to call
    $testing(); // calling the function (in this case outputs "test!")
 
?>

You can also do this with class methods:

<?php
 
    class test {
       function a() {
          echo "test!";
       }
    }
    $method = "a"; // define which method we're going to call
    $obj = new test(); // create a new object of the "test" class
    $obj->$method(); // calls the "a" method
 
?>

Conclusion

Now you are ready to start developing applications in PHP! PHP is a very flexible and user-friendly language. PHP.net is the place to go when you are looking for help. Great documentation with a lot of user-generated content.

My last advice: write good documentation for your code.

I hope you enjoyed this article.

General Resources

XAMPP - Apache, MySQL, PHP and Perl

Eclipse PDT - PHP IDE

PHP Frameworks

Analysis and comparison of PHP frameworks

Nuno Freitas
Posted by Nuno Freitas on March 19, 2008

Related articles