Upgrading to php 7

If you are PHP developer then you know that PHP 7 is available and you can use it in your projects. PHP 7 comes with a lot of nice new features, but a lot of people are having headache because of backward compatibility. In this post, I will write about things that are not compatible with PHP 7 and you should be careful about.

Probably you are asking yourself, what happened with PHP 6, why PHP jumped from PHP 5 to PHP 7 (they tried to copy Microsoft who jumped from Windows 8 to Windows 10)?

If you didn’t know, PHP 6 existed as a real project which was developed between 2005 and 2010, but it was never officially released. Some features from PHP 6 were later on introduced in PHP 5.3 like closures and namespaces, but PHP internals decided that they should immediately jump to PHP 7.

Timelines

Now when PHP 7 is released, the question is what will happen with PHP 5.5 and 5.6. We will see the end of life for PHP 5.5 on July 10, 2016. When we are talking about PHP 5.6 then only security fixes are going to be available until August 2016 and its end of life is scheduled to take place a year later.

All versions prior to 5.5 has already reached their end of life, without receive even security fixes.

Action: If you are working with PHP < 5.5 you should think about installing PHP 5.6 or even PHP 7 immediately.

Deprecated features

There are a lot of functions that were marked as deprecated in PHP 5.x. If you want to upgrade to PHP 7, you should know that these deprecated functions are removed and not supported anymore. A feature is marked as deprecated to warn developers that it will be removed in some future version of a language. If you are seeing E_DEPRECATED error across you project, then you are using of the deprecated features (functions).

Alternative PHP tags

You probably know about standard PHP tags like <?php or <?= but there are other tags that you may not even be aware of. Before, you could enclose your PHP code in <script> tags like it is demonstrated below:

If you have worked in ASP then you know that you could enclose your PHP code inside ASP Script tags:

If you are using any of these, you will have to change them.

Multiple default cases in switch statement

If you didn’t know, PHP language specification had a bug! You could define multiple default: cases in a single switch statement, but only last default: case would execute which could lead to bugs. For example:

Only the last default: case would execute. This is removed in PHP 7 and you will get a fatal error if you try to write similar code.

The Original MySQL extension is removed

The original ext/mysql extension is removed and not supported in PHP 7, which means that all mysql_ functions are not supported anymore. You realize now that this is the largest change to your existing code. If you want to work with PHP 7, then you have to change all your mysql_ functions to procedural mysqli_ functions or to an object oriented PDO.

Migration to mysqli

Fortunately myslq_ and mysqli_ functions are very similar. The only difference is in letter ‘i’ that you should append to your mysql_ functions. In most cases you will just have to change function name. Here is the list of the incompatible functions between mysql_ and mysqli_:

ext/mysqli supports both functional and object oriented approach API. Which one to use is your decision, but I suggest you to use object oriented API.

PDO API

Instead of ext/mysqli you can use ext/pdo which stands for PHP Data Objects. PDO provides object oriented approach and gives you some additional functionalities likes prepared statements and security.

PDO helps you to connect to variety of databases using consistent API, and you only have to learn one API to work with many databases. Here you can find more information about PDO.

Here is a simple example:

 

Things you should know when upgrading to PHP 7http://code-epicenter.com/wp-content/uploads/2016/01/Upgrading-to-php-7.pnghttp://code-epicenter.com/wp-content/uploads/2016/01/Upgrading-to-php-7-150x150.pngAmir DuranPHPProgrammingPHP,PHP 7
If you are PHP developer then you know that PHP 7 is available and you can use it in your projects. PHP 7 comes with a lot of nice new features, but a lot of people are having headache because of backward compatibility. In this post, I will write about things...