bookmark_borderHow to update PHP 5 to 7?

Updating is never 100% painless, but the team behind PHP has tried its best to lower the incompatibilities and provide a good experience during the process of updating from PHP 5 to 7. 

Migration tools 

Before moving your application from PHP 5 to 7, you should most definitively back up. You don’t want to lose your work. You can also use various migration tools that can tell you in advance what part of the code most probably won’t work and how to fix it. Check one of these 3: 

PHP 7 MAR

php7mar is a simple tool that generates reports. It will show which line of code has a problem, put a note and suggest a course of action. 

PHP 7 Compatibility Checker

php7cc is another similar tool for compatibility checking. It will show errors in red, which could be fatal, syntax, or notices. It will also display warnings in yellow. 

PHPto7aid

php7aid is showing directly what part of your PHP 5 code won’t work. It will try to help you resolve the problems. 

Updating PHP 5 to PHP 7 on Linux

In this step-by-step guide, we will update PHP 5 to PHP 7 on an Ubuntu computer running LEMP (Linux, Nginx, Mysql, PHP) and a user with sudo permissions. 

Before installing or updating, we need to add the repository, which contains PHP 7 for Ubuntu. Use the following command:

sudo add-apt-repository ppa:ondrej/php 

The Terminal will show you a description of the repository. Press Enter and continue. 

Then you will need to check for updates with this command:

sudo apt-get update

 Finally, we can install PHP 7 with the following command:

sudo apt-get install php7.0-fpm php7.0-mysql 

After the installation of the PHP 7, change the path to the fastcgi_pass to the new PHP 7. You need to change “/var/run/php5-fpm.sock” to “/var/run/php/php7.0-fpm.sock”. The result should look like this:

location ~ \.php$ {

 try_files $uri =404;

 fastcgi_split_path_info ^(.+\.php)(/.+)$;

 fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

 fastcgi_index index.php;

 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

 include fastcgi_params;

 } 

Now that we are ready, restart the Nginx:

sudo service nginx restart 

Ready! 

Updating PHP 5 to PHP 7 on macOS

To update PHP 5 to 7 on macOS, we are going to use Homebrew. If you don’t have Homebrew installed already, you can follow these steps to get it. 

Open the Terminal application. 

Paste the following command onto the Terminal and press Enter: 

/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)” 

Press Enter again to agree and then enter your username and password. 

Now you are ready to use Homebrew.

Again, inside the Terminal, you will need to use a few commands:

brew install openldap libiconv

brew tap exolnet/homebrew-deprecated

brew unlink php56

brew install [email protected]

There are no older versions left inside Homebrew’s repositories, so here we are installing directly PHP 7.4 that is still available. In this case, we have unlinked an older version PHP 5.6, before installing the new one. If you have another previous version, you can unlink it. 

Updating PHP 5 to PHP 7 on Windows

If you are a Windows user and you are using XAMPP or WAMP, just update your software. The newer version will have the latest PHP version. You can also go to their websites and download the right version for you. There you can see with which PHP version they are coming before downloading and installing them. 

Updating on CentOS, Fedora, Red Hat

For those of you running any of these OSes, you will need to follow these commands: 

sudo yum update

rpm – Uvh https://dl.fedoraproject.org/pub/epel/epel/epel-release-latest-7.noarch.rpm

rpm – Uvh https://mirror.webtatic.com/yum/e17/webtatic-release.rpm

sudo yum install php70w

sudo yum install php70w-mysql

Consider updating from PHP 5 to PHP 8 directly. 

There will be support for PHP 7.3 until 06.12.2021 and PHP 7.4 until 28.11.2022. Consider making the jump directly to PHP 8 so you can get the newest features and longer support. 

bookmark_borderWhat is the difference between PHP 5 and PHP 7?

The Hypertext Preprocessor (PHP) is a very popular scripting language. It’s been a standard since 1994 due to its attractive features. Safer connectivity to the database, faster speed, runs smoothly on the most used platforms (Windows, Unix, Linux). Of course, there were many improvements, but let’s have a closer look at PHP 5 and PHP 7.

PHP 5 main features.

PHP5 was developed considerably more robust than PHP 4. The Zend II engine powered it. It offered an improved mechanism for handling exceptions and errors, a model oriented to objects that supplies interfaces, abstract classes, constructors, static and final properties. Besides, it had SSL connections, multi-query functions for MySQL extension. PHP5’s XML extensions were enhanced, just like the soap implementation for providing and consuming web services, speed, and performance.

PHP 7 main features.

PHP 7 was the successor of PHP 5. Its performance was better than the PHP 5. It was powered by a PHP-NG (Next Generation) engine. It achieved twice the PHP 5 performance and 50% less memory consumption to give a reference. It added anonymous classes, removal of deprecated methods, better exception hierarchy and handling, more efficient memory use, abstract syntax tree, and support for return type declarations. Besides, it included scalar type declarations for better controlling the code, CSPRNG functions, spaceship operator (three-way comparison), constant arrays using define (). 

What is the difference between PHP 5 and PHP 7?

  • Performance. And PHP 7 speed got double than the one offered by PHP 5. While PHP 5 (Zend II engine) handled an average of 22 requests per second, PHP 7 (PHP-NG engine) made 44. So PHP 7 totally boosted the scripting performance and websites’ loading speed.
  • Exception handling. Handling fatal errors with PHP 5 was hard for developers, mostly because errors were not displayed directly. You needed to trace the information to find it and to identify if it was fatal or not for an application. On PHP 7, important errors became exceptions to be caught easier.
  • Return type. PHP5 didn’t include a provision for return type declarations. This feature was added to PHP 7 version, making the code more accurate. The available return types were: float, string, bool, int.
  • Null coalescing operator ?? On PHP 5, you had to write explicit code to return the null value in the case the value was not available (it didn’t exist). On PHP 7, you could just use the operator ?? (two question marks) to return a variable’s value if it was available or for returning null if it was not available.
  • Spaceship operator (combined or three-way comparison). With PHP 5, many different operators were used for comparing. PHP 7 integrated the three-way comparison operator (<=>), meaning that with a single operator, three kinds of comparisons were possible to do simultaneously: greater than, less than, and equal. It returned 0, meaning equal, -1 if the right part was greater than the left, and 1 in the opposite scenario.
  • Declarations. PHP 5 worked with individual declarations for namespaces. PHP 7 simplified the task through the use of group declaration. This made code more compact and easy to read. It definitely meant less time typing for programmers. 
  • 64-bit support. While PHP5 didn’t support 64-bit, PHP7 did, the complete 64-bit and large files. Running applications smoothly on these architectures was not a problem anymore.
  • Anonymous class. It was included on PHP 7. Then, creating a complete definition of a class required just once in the whole application was not needed anymore. Instead, an anonymous class for a single-use got possible. 

Conclusion.

The jump from PHP 5 to PHP 7 was key for PHP to evolve and become a more useful scripting language. Currently, we are on PHP 8 (8.0.8 version). Stop imagining the possibilities. Better experience them now!

bookmark_borderPHP Version 5 – everything you need to know

PHP 5

PHP 5 was released on 1 July 2004. That is 4 years after introducing PHP 4 to the scene of the Internet. The aim is to bring a lot of new functionalities to the PHP language. The focus of the team was on what is not very well supported and what is missing in the previous versions. The focus of the new PHP 5 version is on 3 major areas: Object-Oriented programming, XML, and MySQL support.

Object-Oriented programming

This feature was present in PHP 4, but in PHP 5 it is introduced an entirely reworked object model. It now offers:

  • Interfaces
  • Destructors
  • Constructors
  • Protected, private, and public properties and methods
  • Class type hints
  • Abstract classes
  • Final properties and methods
  • Static properties and methods
  • Magical methods

Programmers in PHP 5 are able not just to pass objects by value but also by reference.

Reworked MySQL extension

The MySQL extension is completely reworked in PHP 5. It provides support for MySQLi, which is the new version of MySQL. It means MySQL improved. This new extension offers:

  • Bound input and output parameters
  • Prepared statements
  • SSL connections
  • MySQL can now apply PHP’s new OO model
  • Multi-query functions

Reworked XML extension

XML tools were great for the job in PHP 4. Except, they were not actually designed to operate together, and often patches were necessary to complete the required functionality. In PHP 5 is applied a reworked XML parser with tools capable of operating with one another. Furthermore:

  • They are included in a single XML library: libxml2.
  • XML extensions can operate together as a whole.
  • They provide efficient data processing. 
  • XML extensions can provide you with the exact XML tool for the task.
  • They comply fully with W3 specifications.

The SimpleXML extension is also introduced in PHP 5. It allows you to handle the data in XML documents easily. It treats it as an array and looping through it.

Release history of PHP 5

5.0 version. PHP 5 was released powered by Zend Engine II with a new object model.

5.1 version. For accessing databases was added PHP Data Objects (PDO) as a lightweight, consistent interface. Improvements in performance with the introduction of compiler variables in re-engineered PHP Engine. 

5.2 version. Native JSON support. The filter extension is enabled by default. 

5.3 version. New features are Namespace support, late static bindings, jump label. Also, the Windows support is improved. Further added features like, sqlite3, and mysqlnd replacing libmysql as an underlying library for the extensions that operate with MySQL.

5.4 version. New features are trait support and short array syntax support. In addition, some improvements are made to the current performance and features, plus decreased memory requirements.

5.5 version. Further support for generators and finally blocks for exceptions handling. OpCache (based on Zend Optimizer+) in a set in the official distribution.

5.6 version. Constant scalar expressions, possible large file uploads, new interactive “phpdbg” debugger as a SAPI module, and other more minor improvements.

Is PHP 5.6 still supported?

If we have to give a short answer, it is “No.” This is because PHP developers are no longer supporting PHP 5.6 or any other version of PHP 5. On 31 December 2018, officially ended the support of PHP 5.6. 

Furthermore:

  • There won’t be any more security updates.
  • There won’t be any more bug fixes.
  • In a production environment, you should not use PHP 5.6 or any version of PHP 5. 
  • For security reasons, you should no longer use PHP 5. There are many out-of-date libraries and functions.