Everything you need to know to get started with PHP

Everything you need to know to get started with PHP
HIGHLIGHTS

The power behind the world’s most powerful websites

Introduction

In 2004, when Mark Zuckerberg sat down to create the site that needs no introduction, he chose PHP. Moving forward the code grew and it is currently one of the biggest websites the world has ever seen. If you know the early history of Facebook, you’d know that it was not meant to be the behemoth that it is today. There had to be something wise about Mark to choose PHP for the task and something equally incredible for PHP to carry his website to create a world-wide-phenomenon. Probably it is the same thing in PHP which attracts or attracted anyone else to work with it. 
Welcome to the world’s most loved and most hated programming language – PHP.

Learn php tutorial
PHP is the world’s most popular server-side scripting language

History

PHP was originally called PHP/FI (Personal Home Page/Forms Interpreter) in 1994 when it was originally designed and was renamed to PHP Tools in 1995 before it was renamed to just PHP (as a recursive acronym meaning PHP Hypertext Preprocessor) in 1997 with the release of version 3.0. The initial name of PHP clearly suggests that it was not designed to be what it appears as today – the king of web programing languages. In fact, its creator Rasmus Lerdorf built it as a set of CGI binaries which would help him program his home page better with some database support. It actually wasn’t meant to become a full-fledged programming language.

The initial public release of PHP in 1995 had, quite surprisingly, a large set of the basic capabilities that it still has today, and made mostly internal improvements across versions. It added object oriented programming capabilities in its version 5.0 release, done in year 2004. Since then a lot of features have been added to PHP including namespace, late static binding and closure support in version 5.3 and traits in 5.4 with a large number of features in other releases bringing it at par with other programming languages.

Significance and programming

PHP is known for being one of the easiest programming (or scripting) languages to start off with, and is thus used by a large number of programmers who are starting out. 

Another point where PHP shines out is being an almost-web-exclusive language. While PHP can be easily and effectively used for system scripts, it was built for handling websites and flaunts features which address issues specific to web development such as handling forms submission, user sessions, cookies and loose data types. PHP is designed to work in a ‘single request-single response’ environment where the script starts working when a request comes in, processes the request, generates the response, sends it to the client and then exits. The next time the same script is requested, it runs again, serves the request and again ends the execution.

Learn php tutorial
The world’s most popular social networking website runs on PHP

This model suits the architecture of the web where a user makes requests sporadically to the server. At the same time, it can also be used to create sustainable services, which keep running indefinitely. This makes PHP suitable for working with WebSockets API introduced in PHP5. PHP allows the developer to mix HTML and PHP together while also being suitable for coding in a number of software patterns. Primary among those is the MVC software pattern, which suits the architecture of web very well (almost all PHP frameworks are modeled in MVC pattern). This flexibility helps in fixing 

PHP powers more than 80% of all websites in the world with ASP.NET holding the second position with a little over 15% of the share!  Most popular blogging tools and content management systems from wordpress and drupal to MediaWiki and OwnCloud run PHP. Also, some of the world’s most popular websites including Facebook, Wikipedia, WordPress.com and Yahoo! are powered by PHP. With the world moving towards the “Internet of Things” where a lot of server-side connections will be required, PHP might push its dominance even further.

Learn php tutorial
PHP powers more than 80% of sites across the globe!

Future

It is planned that the next major version will be named version 7.0 (and not 6.0) to avoid confusion with content in books that were already written for PHP 6.0, which was later released as version 5.3. Version 7.0 will address a long known drawback – confusing order of parameters for built-in functions. Also, inspired by Facebook’s HHVM interpreter for PHP, the PHP engine (parser and executor) is being overhauled to bring in lots of speed improvements. Certain tests indicate about 100% performance improvement over the current version while largely maintaining the existing syntax and API. The future looks promising for PHP. 

Show me the code

The first entry to any programming language starts with a ‘Hello world’ program. Here is how it looks in PHP: 

<?php echo “Hello World”; ?>

But that is too simple. Right? Let’s try to print something little more interesting:

<?php 
$x = 5;
$x_square = $x * $x;
$result = “The square of $x is: “ . $x_square;
echo $result;
?>

This small program shows a good deal of features of PHP which make web development easy. The output of this program is: 

Value of $x is 5 The square of 5 is: 25

Line 2 shows how you can dynamically declare a variable and assign it a value. Notice that there is no data type for the variable. Data type is automatically determined.
Line 3 shows you can perform simple mathematical operations as easily as you do in any other programming language. Once again, creating the variable is dynamic.
Line 4 shows that a string can be concatenated (using the ‘.’ operator) with an integer ($x) and printed on screen without any effort. PHP does the type conversion for display so you do not have to worry about it.
Line 5 shows that inside a double quoted string, a variable is evaluated before printing. Also, concatenation of a string and an integer can be stored into a variable and the resulting variable can be printed (line 6) and will be shown without an error.
Isn’t it interesting?

Let’s move to creating an array, sort it and display it:
<?php 
$arr = [1.5, “4”, “10.83”, 2];
sort($arr);

foreach($arr as $x){
echo $x . ‘, ‘;
}
?> 

The output of the above program is: 

1.5, 2, 4, 10.83, 

This program creates an array with 4 elements, which can all be read as numbers (floats and integers) but 2 of them are represented as strings (yes, an array can contain different data types in PHP allowing flexibility) and PHP converts them all to numerical form and then does the sort. Also, a ‘foreach’ construct shows that you can handle the elements of an array pretty easily. If you were to introduce a string in that array which cannot be converted to a number, PHP will treat all elements as strings and sorting will happen considering all elements as strings but it would not fail. You can alter the strictness of PHP’s behavior in many cases by adding additional checks. 
We have barely touched the surface of PHP. If it looks interesting, and you would like to learn more, go on reading. 

Tools and resources

While PHP requires you to have a web server on machine, we cannot discuss its installation in detail. Depending on your OS, you can best install PHP as follows: 
1.     Windows: Install XAMPP – this is the best way to start off on Windows.
2.     Mac OS X: PHP and Apache come bundled with OS X. We cannot detail the process here due to space constraints. You can read the official PHP guide for the same here.
3.     Linux: You can install PHP easily using your package manager. Most Linux DVDs also carry PHP with them. On Ubuntu, it is as easy as: sudo apt-get install apache2 php5 libapache2-mod-php5 php5-mcrypt

Once installed, you can start writing code in no time. Any simple text editor will do the job. But in time you would need to get faster. Eclipse with PHP tools is a good free IDE for PHP development. If you want a very professional tool, there is probably no IDE which can beat PhpStorm. However PhpStorm is a paid product which is available to students, lecturers and open source contributors for free – all you have to do is apply for a free license at JetBrains.com. 


PHP’s API is thoroughly documented

 

In the course of learning any language, one of the most important resources is the API documentation of that language. If you are keen to learn PHP, you are lucky – PHP has one of the most useful, easy and detailed API documentations with examples, warnings, best practices guide along with user comments for almost every function in the language (and many other popular extensions).
The popularity also means that you can search for your problems on the web and would usually get a good answer written already. If not, StackOverflow.com is a great place to ask questions. If reading books and understanding the underlying architecture is your style, Programming PHP,  by Rasmus Lerdorf (creator of PHP) himself would be the best you can get for the language. When you go for PHP development, unlike most other programming languages (except Java, probably), you will be exposed to an overwhelming number of frameworks. Remember to read about MVC (Model View Controller) architecture and how it works because moving forward, that might help you a lot. 

For tutorials on 15 other hot programming languages go here.

Vaibhav Kaushal
Digit.in
Logo
Digit.in
Logo