PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language. In general, PHP web applications run on a web server, and serves web pages to visitors on request. One key features of PHP is that you can embed PHP code within HTML web pages, making it very easy for you to create dynamic content quickly.

Why you should you consider using PHP?

First of all, PHP is completely free and it is very easy to learn when compared to other scripting languages.

 

One of the great features of PHP is that it is cross-platform which means that you can run PHP programs on Windows, Linux, Mac OS and Solaris among others. This is also true for web servers as PHP engine can be integrated with all common web servers, including IIS, Apache and Zeus.

 

According to the web site w3techs.com, PHP is used by 82.1% of all the websites whose server-side programming language we know by the time of this writing reflecting its importance on the market.

 

If you search on the web about the advantages and disadvantages of PHP when compared to ASP.NET MVC, for instance, you will note the majority of the answers are that it depends. That is why in my humble opinion I believe that it is important considering this technology when choosing the programming language for your web application.

Integrated Development Environment options

For developers who loves Visual Studio, there is a good and a bad news for you. The good news is that there are some Visual Studio extensions that let you develop PHP web application using Visual Studio, taking advantage of its main features. Nevertheless, the bad news is that I couldn’t find any free extension. One of them is called “PHP Tools for Visual Studio”. After installing this extension, you will realize that you can only use it for free during a small period of time to test it. For more information please visit this page.

 

For this reason, I will show you how to develop PHP using Eclipse. I am going to write some blog posts about Java so this is a good opportunity for you to get started with Eclipse, one of the main IDE for Java.

Installing PDT on Eclipse

Eclipse is an integrated development environment (IDE). It contains a base workspace and an extensible plug-in system for customizing the environment. It is mostly used for Java development but it can be used to PHP development as well. If do not have Eclipse, you can download it from Eclipse web site.

 

In order to develop PHP in Eclipse, one of the options is to install the PDT (PHP Development Tools) package.  Please follow the procedure in order to install this package:

  1. Open Eclipse (my version is Kepler Service Release 2).
  2. Go to Help --> Install New Software...
  3. Expand the "Work with" drop down and select: "Kepler - http://download.eclipse.org/releases/kepler".
  4. Expand "Programming Languages" from the list.
  5. Check PHP Development Tools (PDT) SDK Feature.
  6. Click "Next >" at the bottom and follow the further instruction of Eclipse.
  7. After successful installation of PDT: Go to Window --> Preferences and make sure you will find PHP listed on the left panel.

Installing XAMPP

On the previous section, it was shown how to install the package for Eclipse in order to develop in PHP. Nevertheless, there are many products that still need to be installed like Apache HTTP Server and PHP binaries. In this blog post, we are going to install it using a solution package called XAMPPP, which is a free and open source cross-platform web server solution stack package, consisting mainly of the Apache HTTP Server, MySQL database, and interpreters for scripts written in the PHP and Perl programming languages.

 

In my case, I have downloaded version 1.8.1 from this website but more recent versions should work as well. After the download is complete just execute the setup and follow the steps.

Setting up XDebug

If you want to have the possibility to debug PHP code within Eclipse you should configure XDebug or Zend. In this blog post, we are going to set up the XDebug.

 

XDebug is an opensource Debugger and Profiler for PHP. PDT has built in support for XDebug, which allows you to step-debug through your PHP projects. It is already included on the XAMPP solution.

 

In order to enable XDebug, the file php.ini located on C:\xampp\php\php.ini needs to be edited as shown below:

 

 

 

[XDebug]
zend_extension = "C:\xampp\php\ext\php_xdebug.dll"
xdebug.profiler_append = 0
xdebug.profiler_enable = 1
xdebug.profiler_enable_trigger = 0
xdebug.profiler_output_dir = "C:\xampp\tmp"
xdebug.profiler_output_name = "cachegrind.out.%t-%s"
xdebug.remote_enable = 1
xdebug.remote_handler = "dbgp"
xdebug.remote_host = "127.0.0.1"
xdebug.trace_output_dir = "C:\xampp\tmp"

 

 

 

After saving php.ini, restart Apache through XAMPP Control Panel as shown on Figure 1.

 

8446.fig1.jpg

 

Figure 1 – XAMPP Control Panel used to start and stop Apache.

 

 

 

 Open Eclipse and make sure that the workspace you are working is: \xampp\htdocs.  This folder is the root folder of your web server.

 

When Eclipse is loaded, click on WindowàPreference. On the left pane, select PHP à Debug. Under Default Settings, choose XDebug for the PHP Debugger as shown on Figure 2 and then click “Apply” and then “OK”.

 

 

 

 8015.fig2.jpg

 

Figure 2 – PHP Debug settings on Eclipse.

 

 

 

At this point, you shall be ready to create your first PHP web application.

 

I have written this procedure by watching this video on youtube. Watching this video might help you understand better even though we are using more recent product versions in case you have questions.

Your first PHP program

On package explorer located on the left pane of Eclipse, right-click and select Newà Project…

 

On the New Project window, select PHP Project under PHP folder. Type on the project name “HelloWorld” and then click on finished making sure that the project will be created under the folder C:\xampp\htdocs.

 

Now, there is a folder HelloWorld on Package Explorer. Right click on it and select New à Other. A window called New will open where you should select “PHP File” and name this new file helloworld.php.

 

 

 

Copy and paste the following content of helloworld.php:

 

 

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Greetings</title>
<link rel="stylesheet" type="text/css" href="common.css" />
</head>
<body>
<?php
echo "<h1>Hello World!!</h1>";
$hour = date ("G");
if ($hour >= 5 && $hour < 12) {
      echo "<h1>Good morning!</h1>";
} elseif ($hour >= 12 && $hour < 18) {
      echo "<h1>Good afternoon!</h1>";
} elseif ($hour >= 18 && $hour < 22) {
      echo "<h1>Good evening!</h1>";
} else {
      echo "<h1>Good night!</h1>";
}
?>
</body>
</html>

 

 

At this point, your project in Eclipse should be similar to the screenshot shown on Figure 3.

 

 

 

2548.fig3.jpg

 

Figure 3 – HelloWorld project in Eclipse

 

 

 

This is a good example for showing that PHP code is embedded within the HTML markup. If you read the code snippet carefully, you will note that the application will show “Good morning!”, “Good afternoon!”, “Good evening!” or “Good night!” according to the value of the variable $hour, which corresponds to the current hour. The function date("G") is responsible for getting the current hour of the day.

 

If you insert a breakpoint on line 13 and debug your project, you will be able to check the values stored by the variables on the variables window as shown on Figure 4.

 

3755.fig4.jpg

 

Figure 4 – Debugging HelloWorld PHP project in Eclipse

Conclusion

This purpose of this blog post is to help you set up your environment to develop PHP web applications. On the next blog post, we are going to develop the same web application that receives data from the PI System through PI Web API but not using jQuery \JavaScript but PHP. The advantages and disadvantages of this this options are going to be discussed as well. Stay tuned!!