Getting Started with PHP, the Popular Programming Language

0
7629

This article provides an introduction to PHP — the development process, its history, as well as its pros and cons. At the end of the article, we learn how to install XAMPP on a computer, and to write code to add two numbers.

If you refer to any Web technology survey to check the market share of different server side scripting languages, you will be surprised to know that PHP is used by an average 70 per cent of the websites. According to w3techs.com, “PHP is used by 82.7 per cent of all the websites whose server-side programming language we know.” In the early stages, even Facebook servers deployed PHP to run their social networking application. Nevertheless, we are not concerned about the Web traffic hosted by PHP these days. Instead, we will delve deep into PHP to understand its development, its history, its pros and cons and, in the end, we will have a sneak peek into some of the open source IDEs which you can use for rapid development.

First, let’s understand what PHP is. It is an abbreviated form of ‘Hypertext Pre-processor’. Confused about the sequence of the acronym? Actually, the earlier name of PHP was ‘Personal Home Page’ and hence the acronym. It is a server side programming language mainly used to enhance the look and feel of HTML Web pages. A sample PHP code embedded into HTML looks like what follows:

<!DOCTYPE HTML>

<html>

<head>

<title>Example</title>

</head>

<body>

<?php

echo “I am PHP script!”;

?>

</body>

</html>

In the above example, you can see how easily PHP can be embedded inside HTML code just by enclosing it inside <?php and ?> tags, which allows very cool navigation between HTML and PHP code. It differs from client-side scripting languages like JavaScript in that PHP code is executed on the server with the help of a PHP interpreter, and only the resultant HTML is sent to the requester’s computer. Though it can do a variety of tasks, ranging from creating forms to generating dynamic Web content to sending and receiving cookies, yet there are three main areas where PHP scripts are usually deployed.

  • Server-side scripting: This is the main usage and target area of PHP. You require a PHP parser, a Web browser and a Web server to make use of it, and then you will be able to view the PHP output of Web pages on your machine’s browser.
  • Command line scripting: PHP scripts can also be run without any server or browser but with the help of a PHP parser. This is most suited for tasks that take a lot of time — for example, sending newsletters to thousands of records, taking backups from databases, and transferring heavy files from one location to another.
  • Creating desktop applications: PHP can also be used to develop desktop based applications with graphical user interfaces (GUI). Though it has a lot of pain points, you can use PHP-GTK for that, if you want to. PHP-GTK is available as an extension to PHP.

Fact: Did you know that PHP has a mascot just like sports teams? The PHP mascot is a big blue elephant named elePHPant.

PHP and HTML – similar but different

PHP is often confused with HTML. So to set things straight, let’s take a look at how PHP and HTML are different and similar at the same time. As we all know, HTML is a markup language and is the backbone for front-end Web pages. On the other hand, PHP works in the background, on the server, where HTML is deployed to perform tasks. Together, they are used to make Web pages dynamic. For better understanding, let’s look at an example where you display some content on a Web page using HTML. Now, if you want to do some back-end validation on the database, then you will use PHP to do it. So both HTML and PHP have different assigned roles and they complement each other perfectly. Listed below are some of the similarities and differences that will make this clear.

Similarities Differences
Compatible with most of the browsers supporting their technologies.

HTML is used on the front-end whereas PHP is back-end technology.

Can be used on all operating systems.  PHP is a programming language, whereas HTML is called a markup language and is not included in the category of programming languages because it can’t do calculations like ‘1+1=2’.

History and development

The development of PHP dates back to 1994 when a Danish-Canadian programmer Rasmus Lerdorf created Perl scripts called ‘Personal Home Page Tools’ in order to maintain his personal Web pages. The succeeding year, these tools were released under the name of ‘Personal Home Page/Forms Interpreter’ as CGI binaries. They were enabled to provide support for databases and Web forms. Once they were released to the whole world, PHP underwent a series of developments and modifications, and the result was that the second version of ‘Personal Home Page/Forms Interpreter’ was released in November 1997. Moving on, PHP 3, 4 and 5 were released in 1998, 2000 and 2004, respectively.

Today, the most used version of PHP is PHP 5, with approximately 93 per cent of the websites using PHP making use of it, though PHP 7 is also available in the market. In 2010, PHP 5.4 came out with Unicode support added to it.

Figure 1: Apache service started on XAMPP control panel

The pros and cons of PHP

Before going further into PHP development, let’s take a look at some of the advantages and disadvantages of using it in Web development.

Advantages

  • Availability: The biggest advantage of PHP is that it is available as open source, due to which one can find a large developer community for support and help.
  • Stability: PHP has been in use since 1995 and thus it’s quite stable compared to other server side scripting languages since its source code is open and if any bug is found, it can be readily fixed.
  • Extensive libraries: There are thousands of libraries available which enhance the abilities of PHP—for example, PDFs, graphs, Flash movies, etc. PHP makes use of modules, so you don’t have to write everything from the beginning. You just need to add the required module in your code and you are good to go.
  • Built-in modules: Using PHP, one can connect to the database effortlessly using its built-in modules, which drastically reduce the development time and effort of Web developers.
  • Cross-platform: PHP is supported on all platforms, so you don’t have to worry whether your code written in Windows OS will work on Linux or not.
  • Easy to use: For beginners, learning PHP is easy because of its cool syntax, which is somewhat similar to the C programming language, making it even simpler for those familiar with C.

Disadvantages

  • Not suitable for huge applications: Though PHP has a lot of advantages in Web page development, it still can’t be used to build complicated and huge Web applications since it does not support modularity and, hence, the maintenance of the app will be a cumbersome task.
  • Security: Security of data involved in Web pages is of paramount concern. The security of PHP can be compromised due to its open source nature since anyone can view its source code and detect bugs in it. So you have to take extra measures to ensure the security of your Web page if you are dealing with sensitive data.

Fact: It is estimated that there are approximately 5 million PHP developers worldwide, which is a testament to its power.

Open source IDEs for PHP development

The choice of IDE plays an important role in the development of any program or application but this aspect is often neglected. A good and robust IDE comes packed with loads of features and packages to enable rapid development. Automatic code generation, refactoring, organising imports, debugging, identifying dead code and indentation are some of the advantages a powerful IDE can provide. So let’s take a look at some dominant open source IDEs that can be very useful in PHP development.

1. NetBeans: Most of you must be aware of NetBeans in Java development but it can also be used for PHP development. The biggest advantage of NetBeans is that it supports many languages like English, Chinese, Japanese, etc, and can be installed smoothly on any operating system. Some of the features that differentiate it from the rest are smart code completion, refactoring, try/catch code completion and formatting. It also has the capability to configure various PHP frameworks like Smarty, Doctrine, etc. You can download it from netbeans.org.

2. Eclipse: Eclipse tops the list of popular IDEs. If you have worked with Eclipse earlier, then you will feel at home using Eclipse PDT for PHP development. It can be downloaded from eclipse.org/pdt. Some of its features are syntax highlighting, debugging, code templates, validating syntax and easy code management through Windows Explorer. It is a cross-platform IDE and works on Windows, Linux and Mac OS. Since it is developed in Java, you must have it installed in your machine.

3. PHPStorm: PHPStorm, developed by JetBrains (the same company that developed IntelliJ IDEA for Java), is mainly used for professional purposes but is also available licence-free for students, teachers and open source projects. It has the most up-to-date set of features for rapid development since it provides support for leading front-end technologies like HTML5, CofeeScript, JavaScript and Sass. It supports all the major frameworks available in the market like Symfony, CakePHP, Laravel and Zend, and can also be integrated with databases, version control software, rest clients and command line tools to ease the work of developers. A number of MNCs, like Wikipedia, Yahoo and Cisco, are making use of PHPStorm for PHP development. You can read more about PHPStorm at jetbrains.com/phpstorm.

4. Sublime Text: Sublime Text is basically a text editor, but it can be converted into a PHP IDE by installing various available packages. It is known for its sleek, feature-rich and lightweight interface. It is also supported on all operating systems. Some of the packages which can be used to convert it into an IDE are Sublime PHP Companion, PHPCS, codIntel, PHPDoc, Simple PHPunit, etc. It can be downloaded as open source from sublimetext.com.

5. PHP Designer: This IDE is only available for Windows users. It is very fast and powerful, with full support for PHP, HTML, JavaScript and CSS. It is used for fast Web development due to its features like intelligent syntax highlighting, object-oriented programming, code templates, code tips and debug manager, which are all wrapped into a sleek and intuitive interface that can also be customised according to various available themes. It also supports various JavaScript frameworks such as JQuery, ExtJs and Yui. An open source version of it is available and you can read more about it on its official website.

6. NuSphere PHP IDE: PHpED is the IDE developed by NuSphere, a Nevada based company which entered the market way back in 2001. The current available version of PHpED is 18.0 which provides support for PHP 7.0 and almost all PHP frameworks. This tool also has the ability to run unit tests for the developed projects and comes packaged with the support for all Web based technologies. You can download PHpED from NuSphere’s website www.nusphere.com.

7. Codelobster: Codelobster also provides a free IDE for PHP development. Though it is not used too often, it is catching up fast. By downloading the free version, you get support for PHP, JS, HTML and CSS. It can be integrated with various frameworks such as Drupal, WordPress, Symfony and Yii. You can download it from www.codelobster.com.

Figure 2: Screenshot of the list of files in your directory
Figure 3: PHP program when run on the browser

Writing the first PHP program

Having read about PHP, its history and various IDEs, let’s write our first PHP program and run it using XAMPP. Though there is no official information about the full form of XAMPP, it is usually assumed to stand for cross-platform (X), Apache (A), MariaDB (M), PHP (P) and Perl (P). XAMPP is an open source, widely used Web server developed by apachefriends.org, which can be used to create a local HTTP server on machines with a few clicks. We will also be using it in our tutorial below.

  • Download the latest version of XAMPP on your systems from the website https://www.apachefriends.org/download.html. After the download is complete, install it in your machine.
  • By default, XAMPP is installed in your machine’s C drive, but if you have specified any other directory in the installation process, go to that directory and create a folder named PHPDevelopment inside the htdocs folder in your XAMPP installation directory folder. For example, C:\xampp\htdocs\PHPDevelopment.
  • Now start the XAMPP control panel and click on the Start button to start Apache.
  • Create a text file inside the above folder named AddTwoNumbers.php and copy the following code inside it:
<!DOCTYPE html>

<html>

<head>

<title>Sum</title>

</head>

<body>

<h3>Addition Of Two Numbers</h2>

<form>

<div>Number 1:</div>

<input type=”text” name=”num1”/>

<div>Number 2:</div>

<input type=”text” name=”num2”/>

<div><br><input type=”submit” value=”CALCULATE SUM”></div><br>

</form>

<?php

if (isset($_GET[‘num1’]) && isset($_GET[‘num2’])) {

$num1 = $_GET[‘num1’];

$num2 = $_GET[‘num2’];

$sum = $num1 + $num2;

echo “Sum of $num1 and $num2 is $sum”;

}

?>

</body>

</html>
  • Now type localhost/ PHPDevelopment and it will list all the files in your directory on your browser, as shown in Figure 2.
  • Click on AddTwoNumbers.php and you will be directed to the required page, where you can perform the addition of two numbers.

Here, you can see that the form has been created using HTML and the corresponding addition of the numbers is done using PHP. Now start your Web development using PHP. You can also make use of the various frameworks available to simplify development and lessen your coding time. Happy coding!

LEAVE A REPLY

Please enter your comment!
Please enter your name here