PHP is an acronym for Hypertext Preprocessor language. Would you like an introduction to PHP?


It is an open-source general purpose scripting language which is used to build dynamic websites. PHP is a cross platform language, which means it can run on different operating systems such as Windows, Linux and Unix and supports different databases like MYSQL, Microsoft Access and Oracle. Due to its cross platform and open source nature it is very versatile and useful.

PHP code

Features of PHP

Here are some important features of PHP which are the main reason of its popularity:

  • First of all, we love PHP due to its cross platform nature and because it is open source.
  • Another reason of its popularity is that it supports most web servers (Linux, Unix, Windows etc);
  • Compared to other programming languages the learning curve of PHP is small.
  • Have you ever visited the PHP community? Not only does it cover any possible subject, but also it is well supported.
  • Integration with HTML is easy.
  • Has built-in support for web services.
  • It can be easily extended with other programming languages.
  • PHP is integrated with a number of different databases like MYSQL, Oracle and Informix.
  • Supports a large number of major protocols such as POP3 and LMAP.
  • It is secure and flexible. 

Uses of PHP  

Here are some major uses of PHP:

  • PHP is used by nearly 80% of all websites including some major platforms like WordPress.  
  • One of the biggest uses of PHP is to design server side applications.  
  • Hypertext Preprocessor (PHP) makes it easy to connect with any kind of databases at the server side.  
  • It is used to send and receive emails.  
  • Furthermore, PHP can be used to build membership sites.
  • It is open source that is why it has a large community support system.

Versions of PHP  

PHP was first developed in 1994. After that multiple other versions have been released. The current version is PHP version 8.0  

Besides, PHP 8 is a major version released on November 26, 2020. It has notable changes from previous versions which are:  

Characteristics of PHP 8  

The following are key features of PHP 8: 

JIT (Just in time)  

JIT is the most prominent feature of PHP 8. PHP JIT is an independent part of OPcache. It can enable and disable at runtime and compile time.

Consider that JIT stands for Just In Time compiler. It is a way of  executing computer code during the execution of the program rather than before execution.

Therefore, JIT translates PHP byte code to machine code. This function has improved performance of applications which has heavy mathematical functions. It increases performance of PHP applications, as during runtime it can compile generated code into  the native machine code. If the JIT is enabled the code will be run by CPU itself, that’s why it makes PHP very fast.
According to RFC the potential to move more code from C to PHP has increased because PHP is sufficiently fast. 

Union types  

Union types are one of very important function in PHP 8 because PHP is a dynamically  typed structure. Currently PHP supports two types of union types, some type of null and  array or traversable. In PHP 8, union types accepts values of multiple types, rather than  a single one which indicates either one of those can be used. 

Attributes 

Attribute function is available in many other languages like  C#, C++, Rust and others.  Before PHP 8, PHP supported an unstructured form of metadata. Now in new version  you can use structured metadata with PHP native syntax. Attributes offer ability to add  machine readable metadata information that can be used to specify properties for  objects, elements or files.  

Error Handling 

Before this latest upgradation PHP emits a warning and return null when encounters a  value which it cannot use. As PHP warning does not halt the remaining block so this  behavior was not desirable. Now in PHP 8 internal functions can throw a exception for  type errors or value errors. Passing an illegal parameter to a user defined function is  type error. Now instead of warning, PHP can throw exception.  

WeakMaps 

To improve performance and to prevent memory leaks in long running processes, PHP 8 introduced weakMaps. Weakmap is collection of data objects in which keys are weakly referenced.  A weakmap is caches of data derived from an object that do not need to live longer than object. If the object falls out of scope, it will not prevent the garbage collector from clearing the object. 

Nullsafe operator

Nullsafe is basically short circuiting means skipping the evaluation of an expression based on some given condition. PHP Nullsafe operator is a new feature that provides optional chaining to PHP. It short circuits the retrieval if the value is null, without causing any errors. The null safe operator is ?->

Match Expression 

Match Expression is similar to switch statement l, it has a subject expression which is compared against multiple alternatives. It supports single line expression and doesn’t need a break statement. Match Expression dose strict comparison. 

For example in PHP 7 and older versions: 

For example in PHP 7 and older  versions 
Switch ( 7.0 ) { 
  Case '7.0’ :
           $answer  = “Beautiful”
        Break;
Case 7.0 : 
    $answer = “wonderful” 
Break; 
} 
Echo $answer

In PHP 8 we can write a Match Expression:

Echo match (8.0) { 
      ‘8.0’ => “Beautiful” 
      8.0 => “Wonderful” 

} ;

Getting Started with PHP  

Learning PHP is very easy and interesting. Two things are needed to get started with PHP. One is a development environment to run the PHP script and another is an editor to write PHP code. Before learning PHP you should have some basic knowledge of HTML (Hyper Text Markup Language) as PHP code is embedded in HTML code. 

PHP file  

The file in which you write your PHP code is called PHP file. It has .php extension. A PHP file can contain HTML, CSS and JavaScript code. 

Text  Editors for PHP 

A good programmer knows how much an efficient text editor or an IDE helps in productive programming and managing code. There are numbers of text editors and IDEs available for free to write PHP script. These IDEs help programmers in smart code completion and validate error. Here I will discuss few of them. 

Sublime Text Editor

Sublime text Editor supports many programming and markup languages. You can expand its functionality by adding different plugins. It’s user interface is customizable. It is multilanguage, multi operating system editor. 

[ website | download ]

Dreamweaver

Dreamweaver is a popular PHP editor, it allows you to create and manage website. It has many plus features. Built in HTML validator validates HTML tags. It is used to develop dynamic websites by providing readymade layouts and custom sizes. 

[ website | trial ]

PhpStorm 

PhpStorm is a cross platform IDE built for PHP. It is very suitable for large scale projects and dynamic applications. It’s main features are testing, debugging, code analysis, intelligent coding assistance and code navigation. 

[ website | download ]

Apache Netbeans 

Apache Netbeans is one of the most popular IDE   used to create applications in PHP and java. It is free and cross platform. It supports all standard features such as syntax highlighting, code completion, editor warnings, code navigation and others. 

[ website | download ]

Notepad++

Notepad++ is very simple and easy to use tool. It supports multilingual environment so it can highlight syntax for different languages like HTML, CSS, JavaScript. 

[ website | download ]

Hello  World example in PHP  

So lets get started with hello world example in PHP  

HTML code starts with <html> inside HTML tags <head > and <body> are two main tags. PHP code is written inside body tags. PHP code starts with <?php tag and ends with ?>. PHP statements end with semicolon. 

Development Environment for PHP 

As discussed earlier we need two things to start working with PHP, one is development environment and second is the editor. We have discussed few most common editors for PHP scripting. Now let’s come to the development environment.

Since we know PHP is a server side scripting language, so we need a server to run PHP code. For developing PHP applications we can create local server. There are number of local servers such as XAMP, LAMP, WAMP, MAMP. Let’s discuss LAMP and XAMP. 

  • LAMP

LAMP is one of the most common solution stack, it is acronym for Linux ( operating system ), Apache (HTTP Server), MYSQL (database) and PHP. It is open source development environment used to build dynamic applications and sites. It was one of the first open source software stack and  still considered by many to be the platform of choice for developing new apps.

  • XAMPP 

XAMPP is stack of group of open source packages which are  installed together to build WordPress site offline on a local web server. It is acronym in which X stands for cross platform, A stands for Apache, M stands for MYSQL, P stands for Perl and P stands for PHP. 

In the next tutorial we will discuss setting up the environment for php development.