PHP5 and PHP7 compared

A popular server-side scripting language, PHP is designed for web development. It is an industry standard language that supports 80% of the websites. The latest version, PHP7 claims to be twice as fast as its ancestor, PHP5.

This blog highlights the differences between PHP5 and  PHP7 and also suggests if it’s worth migrating your project from PHP5 to PHP7.

  1. PHP7 shows better performance as compared to PHP5 and simply by upgrading to PHP7, one gets a massive upgrade in performance. Owing to this fact, PHP7 is often referred as PHPNG (PHP – Next Generation) that shows a tremendous increase in the performance of code.
  2. Developers were concerned over their inability to declare a return type for a function in previous versions. This concern is taken care of in PHP7 where  a developer can declare return value type. For example, the following code returns float data type: 

public function areaofcircle (float $r) : float

           {

                 return 3.14*$r*$r;

          }

3. In PHP7, the Spaceship operator is used to compare two expressions. It returns -1, 0, or 1 when the first expression is less. For example, the following code compares the value of a and b in a single statement which was not in case of PHP5 where multiple statements were used to perform same operations.

In PHP5, the code below :

function sort ($a,$b)

       {

               if ($a>$b)

                       return 1;

               else if ( $a ==$b)

                       return 0;

             else

                       return -1;

      }

is replaced with a single statement in PHP 7 as shown below.

function sort ($a,$b)

           {

                 return $a < = > $b;

           }

4. The null coalesce operator (??) replaces the ternary operation with the isset() function. This operator returns the first operand if it exists and not NULL, else it returns the second operand.

For example in PHP5, the following code:

if (isset ($_GET [‘name’]))

      {

               $name = $_GET [‘name’];

      }

      else

               $name = null;

is written in PHP7 as

$name = $_GET [‘name’]?? Null;

5. PHP7 carries a few new features like:

  1. Abstract Syntax Tree – This feature maps plain PHP source code in a tree form, which is reliable and convenient to modify and analyze programmatically as compared to the text-based source.
  2. Closure:: call() –  This new method in PHP7 allows variable binding at call time instead of pre-binding.

Conclusion

PHP7 being the future of PHP, all the applications will have to be upgraded to PHP7 soon. At Laitkor, we help you to make the most of PHP to achieve your business objectives and stay ahead in technology.

Leave a Reply

How can we help you?

Have an experienced software architect provide a free estimate & professional consultation.


Looking for a First-Class Software Consultant?