bookmark_borderGet IPv4 address with PHP explained

Do you know how to get an IPv4 address with PHP from a host? No. Now It is your time to learn it and start using it in your code. It is very simple, don’t worry.

$_SERVER and REMOTE_ADDR

We can get the IP address of the user with $_SERVER in PHP. The information about the user and its activities could help us for security or marketing purposes. 

The most basic way to collect this information in PHP is with REMOTE_ADDR. The answer will be the IP address of the user that is currently on the webpage.

Example code:

<?php  

echo ‘User IP Address – ‘.$_SERVER[‘REMOTE_ADDR’];  

?>  

You might have a problem if the user uses a proxy. In such a case, you can try the following and get the real IP address of the user:

<?php  

    function getIPAddress() {  

    //whether ip is from the share internet  

     if(!emptyempty($_SERVER[‘HTTP_CLIENT_IP’])) {  

                $ip = $_SERVER[‘HTTP_CLIENT_IP’];  

        }  

    //whether ip is from the proxy  

    elseif (!emptyempty($_SERVER[‘HTTP_X_FORWARDED_FOR’])) {  

                $ip = $_SERVER[‘HTTP_X_FORWARDED_FOR’];  

     }  

//whether ip is from the remote address  

    else{  

             $ip = $_SERVER[‘REMOTE_ADDR’];  

     }  

     return $ip;  

}  

$ip = getIPAddress();  

echo ‘User Real IP Address – ‘.$ip;  

?>  

If you use the PHP code above, you will get the real IP address of the user, no matter if he or she is behind a proxy.

Gethostbyname() PHP function

If you want to know the IP address of a host, and you already know its hostname, you can easily use the gethostbyname() function.

PHP will perform a forward DNS lookup for that hostname, and it will show the IPv4 address related to it. This is its syntax:

gethostbyname(string $hostname): string|false

The result will be either the string of the IP address or a false value.

Example code:

<?php

$ip_address = gethostbyname(“google.com”);

echo “IP Address is: “. $ip_address;

?> 

In this case, the hostname is google.com, and the IP address that we got is 173.194.41.67.

gethostbynamel() PHP function

Similar to the previous function with the difference, it will show a complete list of IP addresses of the host.

<?php

$ip_address_array = gethostbynamel(“google.com“);

print_r($ip_address_array);

?>

Conclusion

Now you know how to get the IPv4 address of a host with PHP. You can also do the opposite and get the hostname of a host if you know its IPv4 address with gethostbyaddr(). It could be a good idea to check it out, too, so you have a better and richer knowledge of PHP.

bookmark_borderBest way to get hostname with PHP

Sometimes you have a PHP application and multiple devices, and you need a fast and reliable way to get the hostname of a server. If that is your case, here you will learn the best way to get a hostname with PHP. You can find it if you know the IP address or the local machine is the host.

Gethostbyaddr() PHP function

You can get the hostname if you already have the IP address of it with the gethostbyaddr() PHP function. PHP will perform a reverse DNS lookup for that IP address, and it will show the hostname related to it. This is its syntax:

gethostbyaddr(string $ip): string|false

The result will be either the string of hostname or a false value.

Example code:

<?php

$hostname = gethostbyaddr(“64.15.113.39”);

echo “Hostname is: “. $hostname;

?>

In this case, we are checking the IP address 64.15.113.39, which you can see in the result, belong to the host with hostname – cache.google.com.

There is another very similar function that is the mirror of this one, and it is called the gethostbyname() function. As you could have guessed, you can use it if you have the hostname already, and you need to find the IPv4 address of it.

gethostname() PHP function

The gethostname() PHP function will give you the hostname of the localhost. Its syntax is the following:

gethostname(): string|false

It is an inbuilt function in PHP. There are no additional parameters, and the result can be either string with the hostname if successful or false if not.

It works on PHP 5.3 and newer versions.

Example code:

<?php

echo gethostname();

?>

Conclusion

After this, now you know the best way to get the name of the host using PHP. It is not hard to use it. So go on and try it right away!

bookmark_border​How to check DNS records using PHP

PHP is a very useful programming language, and you can use it, instead of the built-in commands like the Dig command, for querying DNS records. Do you want to know how to check DNS records using PHP? It is a simple process, and there are different PHP functions that you can use. Let’s see them.

​PHP functions for checking DNS records

There are several functions that can be used for checking DNS records. For example, some can check just if the DNS records exist, others serve for only a specific type of DNS record, and others can show any type.

  • checkdnsrr() – This query will see if DNS records exist for the host (hostname or IP address).
  • dns_get_record() – Get the DNS record for the host (hostname or IP address). It could be any type of DNS record.
  • getmxrr() – Get the MX DNS records only, for the host (hostname or IP address).

​checkdnsrr() function 

This will check many different types of DNS resource records. You can specify it with the following parameters: 

  • host – hostname or IP address.
  • type – The particular type of DNS record that you are interested in, like A, AAAA, MX, SOA, NS, CNAME, SRV, TXT, ANY (show all), or another.

The result will be just an answer if there is such a record (“1”) or there is no such a record (“0”). 

Code example:

<?php

$all_records_check = checkdnsrr ( ‘google.com’, ‘ANY’ );

echo $all_records_check;

?>  

This will check the host google.com for any type of DNS records, and the result is that it does have DNS records (“1”). 

<?php  

$mx_records_check = checkdnsrr ( ‘google.com’, ‘MX’ );

echo $mx_records_check;

?>  

This will check the host google.com for MX DNS records, and the result is that it does have MX records (“1”).

​dns_get_record() function

This PHP function will serve you if you actually need to check the content of the DNS records. It will retrieve the DNS record or records that you asked for from the DNS name server. You have a few parameters that we can use as before:

  • hostname – Here, you can put the hostname like “google.com”. 
  • type – With this parameter, you can specify the type of DNS record that you want to get. Here are some of them that you can use: DNS_A, DNS_AAAA, DNS_CNAME, DNS_HINFO, DNS_SOA, DNS_NS, DNS_PTR, DNS_MX, DNS_TXT, DNS_SRV, DNS_NAPTR, DNS_A6, or DNS_ANY.
  • authns – This will populate the authoritative name server with recource records.
  • addtl – This will populate the authoritative name server with aditional records.
  • raw – Query only the requested type of DNS record, instead of looping type by type before going with the additional information.

This is probably the most used PHP function for checking DNS records because it will return you the actual DNS records. Not like in the previous case, just a confirmation if they exist or not. 

Code example:

<?php  

$get_any_records = dns_get_record(“google.com”, DNS_A+DNS_NS);

print_r($get_any_records);

?>

In this case, we are checking google.com for the A records and the NS records.  

​getmxrr() functions

This works only with MX records. This function will get us the MX records for the domain name we are checking. The parameters that can be used with this are below:

  • hostname – The hostname of the target
  • mxhosts – Here, we will see an array of all the available MX records that the query can find. 
  • weight – With this one, it will show the weight information it can find about the order. 

Code example:

<?php

$get_mx_records = getmxrr(“google.com“, $mx_array);

print_r($mx_array);

?>

With this code we will see the MX records for google.com, organized in an array.

​Conclusion

PHP serves to get DNS records. It is very easy to use. Try the functions that we mentioned before with the hostname that you need and the particular DNS records. 

bookmark_borderPHP vs. Python: What is the difference?

What is PHP?

PHP, short for Hypertext Preprocessor, is an open-source, server-side scripting and programming language. The primary usage of it is for web development. However, PHP is a very important language for everybody involved in some way with WordPress. The reason for that is because PHP is used for the majority of the core WordPress software. Many people think that PHP as a programming language is not in use anymore, but actually, that is not quite true.

PHP has different versions, which you can benefit from by installing them on your server. The brand new PHP 7.4 and PHP 8.0 come with a lot of new performance improvements rather than the older versions.

It is essential to choose the most recent versions of PHP not only for performance improvements but also for better security.

What is Python?

Python is a common general-purpose programming language. It is also very well known, and it is possible to be applied for a great range of types of applications. In addition, Python offers for its users dynamic typing, high-level data structures, and a lot of other features. All of these benefits are the reason why Python is used for complex application development, being for scripting or, in other cases, for connecting components together, performing like “glue code.” 

In some cases, it can be extended and put in use for performing system calls to nearly any operating system. Also, in addition, to run a code that was written in C or C++.

Thanks to its universality and capability on almost any system architecture, Phyton is a great choice. So that helps for it to be a universal language and to be seen used in all kinds of various applications.

PHP vs. Python

  • PHP is for Hypertext Preprocessor applied to develop a dynamic website or web application. On the other hand, Python is a high-level object-oriented programming language that is applied for accelerated application development.
  • PHP is usually the choice for a language when we are talking about web development. In the other case, Python is commonly applied in data science, AI, and scientific society.
  • PHP holds a number of frameworks, and Python has a pretty small amount of frameworks.
  • When we are analyzing PHP vs. Python for web development, PHP has an extensive variety of syntax and naming conventions. On the other hand, Python appears with a very concise and precise syntax of codes.
  • The key features of PHP are open source, constant improvements, and simple deployment. For Python, the key characteristics are dynamic typing and rapid development.

Which one to choose?

For sure, deciding which one to choose, PHP vs. Python, is an assuredly not so easy job, even after analyzing the differences between them. The reason is that both of them, PHP vs. Python are great options in their own specific way. Like we mentioned, PHP is applied for web development and server-side scripting. In the other case, Python is the gateway to machine learning codes.

bookmark_borderHow to use PHP code in Linux command line?

You can use PHP code thanks to the CLI SAPI (command-line interpreter/interface Server Application Programming Interface). It can serve you for developing of shell application with PHP, straight from the command line of your Linux computer. 

You can use PHP code in the Linux command line in 3 different ways.

1. Use PHP to execute a specific file.

Let’s say we have a PHP script that is called my_script.php. We can execute it with the following command: 

$ php my_script.php

or 

$ php -f my_script.php

You can use “php” or the “-f” switch. Having the php extension at the end is not required. 

2. Directly pass the PHP code to execute on the command line. 

$ php -r ‘print_r(get_defined_constants());’

You need to be especially careful with shell variable substitution and usage of quotes. In this case, we are using the “-r” switch that does not need tags for beginning or ending. If you add those tags, that will lead to an error. 

3. Execute PHP code via standard input (stdin). 

You can create PHP code dynamically and put it in the binary. See the example below: 

$ some_application | some_filter | php | sort -u > final_output.txt

The PHP binary accepts different arguments, plus the PHP script can receive more arguments. The arguments are not limited by PHP. The arguments are available in the global array $argv (Array of arguments passed to script). The first index should contain the script’s name and the way you are calling it from the command line. If you are using the second (-r) or the third way (stdin), the value of $argv[0] will be “-“. 

$argc, a second global variable, has the number of elements in the array $argv. 

In case the arguments passed to the script do not start with “-“, there is nothing extra to think about. If the character exists, the interpreter will try to handle it, even before the script. You can evade troubles with list separator “–”

# This will not execute the given code but will show the PHP usage

$ php -r ‘var_dump($argv);’ -h

Usage: php [options] [-f] <file> [args…]

[…]

# This will pass the ‘-h’ argument to the script and prevent PHP from showing its usage

$ php -r ‘var_dump($argv);’ — -h

array(2) {

  [0]=>

  string(1) “-“

  [1]=>

  string(2) “-h”

}

Use PHP code on Unix-based system.

Here you will need to take care of one important thing – add the path to the PHP CLI with “#” sign before – #!/usr/bin/php

 Apart from this, everything should work fine. 

Example of PHP script executes as a shell script

#!/usr/bin/php

 <?php

 var_dump($argv);

 ?> 

We asume we are at the current directory and we have a test file: 

$ chmod +x test

$ ./test -h — foo

array(4) {

  [0]=>

  string(6) “./test”

  [1]=>

  string(2) “-h”

  [2]=>

  string(2) “–“

  [3]=>

  string(3) “foo”

}

No worries about the “-” sign. 

In the beginning, the special “#!” shows to the system which program it must use to run it. 

On Windows, you can associate double-click on a .php extension with the php.exe. If the script starts with “#!” it does not bother Windows users because it just takes it as a comment. 

Example of script (script.php), ment to be run from the command line:

#!/usr/bin/php

 <?php

 if ($argc != 2 || in_array($argv[1], array(‘–help’, ‘-help’, ‘-h’, ‘-?’))) {

 ?>

 This is a command-line PHP script with one option.

   Usage:

   <?php echo $argv[0]; ?> <option>

   <option> can be some word you would like

   to print out. With the –help, -help, -h,

   or -? options, you can get this help.

 <?php

 } else {

    echo $argv[1];

 }

 ?>

The program will first check for a required argument (after checking the script name). If there is no argument or there is “–help”, “-help”, “-h”, or “-?” it will print the help message. 

bookmark_borderShould you update to PHP 8?

Yes, you definitely should update to PHP 8, if you are using an older version. There will be many new features, regardless of the version you are coming from. What should you expect?

When was PHP 8 introduced? 

PHP 8 (version 8.0.0) arrived on the 26th of November, 2020. What you should really check about it is the JIT, Named Arguments, Union Types, Match Expressions, Constructor Property Promotion, Nullsafe Operator, Error Handling, and more.

Since then, there have been a few minor upgrades, mainly focusing on stability and bug fixing:

PHP 8.0.1 – 07.01.2021

PHP 8.0.2 – 04.02.2021

PHP 8.0.3 – 04.03.2021

PHP 8.0.5 – 29.04.2021

PHP 8.0.6 – 06.05.2021

PHP 8.0.7 – 03.06.2021

PHP 8.0.8 – 01.07.2021

PHP 5 to PHP 7.2, should you update?

Yes, you should update if you are using any version previous to PHP 7.3. We do not even mention all the new features and bugs fixed that you will get with the new version. The main reason is that the support for all previous versions up to PHP 7.3 is over. They are outdated, and you must update them for better security and compatibility.

Should you update from PHP 7.3 or 7.4 to PHP 8?

The answer is yes. The official support of PHP 7.3 will finish on 06.12.2021, which is getting closer, and those of you who are using 7.4 should consider updating for the new features. Still, if you are on version 7.4, you can use it without problems until 28.11.2022 and get support. By then, PHP 8 will be in its PHP 8.1 version (expected on 25.11.2021), and you can get an even safer and stable version.

What will you get with the new version?

These are the reasons that you should update to the new version 8:

JIT (Just-in-time) compiler. “PHP JIT is implemented as an almost independent part of OPcache. It may be enabled/disabled at PHP compile time and at run-time. When enabled, native code of PHP files is stored in an additional region of the OPcache shared memory and op_array→opcodes[].handler(s) keep pointers to the entry points of JIT-ed code.”

Thanks to the Opcache, the need to load and parse scripts per request is lowered, and that way, you save time and CPU usage.

Constructor Property Promotion – a simplified method for declaring properties. It will lower the repetition of properties and makes it easier to use with objects. A simpler syntax is better for the coders.

Named Arguments – they pass an argument to a function based on the parameter’s name, not its position. Add arguments to a function just by adding the parameter’s name before the value.

Match Expressions – it is similar to the “switch”, but it allows return values, and the new match expression makes identity check comparison (===) instead of a loose comparison (==).

Union Types – it is a useful feature that can unite 2 or more types and show that any of them can be used. In the previous version, the way was to specify union types was only in phpdoc annotations.

Nullsafe operator – with the nullsafe operator, we can finally have behavior like the one of the coalescing operator, on method calls. Use the previously known coalescing operator with array keys and the new nullsafe operator with method calls.

Attributes. They are used to specify different properties for objects, elements, and files in metadata (annotations). Before, you could add metadata only by adding doc-comments.

How to migrate?

You can find a guide on the PHP site, depending on the version you are using. Follow it and see what’s new you will get and if there might be any incompatibilities.

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_border​Why is there no PHP 6?

For computing experts and coders, this is not a mystery. However, for people getting their first approach to Hypertext Preprocessor (PHP), the jump between PHP 5 and PHP 7 can seem strange. Let’s see what exactly happened with this missing version. 

​Why is there no PHP 6?

There is not PHP-6 because it was never released. Its original plan didn’t work as expected. To avoid confusion and not relate new developments (versions) to an unsuccessful experience and negative opinions, the decision was to jump directly from PHP 5 to PHP 7.

​What was the original plan?

The functionality of the PHP scripting language made it really popular among coders. Therefore, the development of new and enhanced versions has kept going since its original release in 1994. Rasmus Lerdorf, its creator, and soon more enthusiast coders joined the effort to improve PHP.

In 2005, PHP 5 was in use, and planning for the next version’s development started. Enthusiasm was big, and plans were ambitious. PHP 6 would include native Unicode support in the API, engine, and extensions. Therefore, no external libraries would be needed, very few conversions required, and language bias would be avoided.

Besides:

  • The safe_mode, together with the “register_globals” function, would be deleted.
  • A new 64-bit integer type was considered.
  • A traits feature for horizontal reuse of the language.
  • Additional static typing features.
  • A goto keyword, avoiding the goto name but adding target labels to break.

What went wrong with PHP 6? 

Efforts to achieve these goals were hard. Especially the Unicode conversion took much longer than expected. And finishing this was key, the base for adding the rest of the new features. As a reference, in 2011 PHP 6 Unicode conversion task advanced around 70%.

To run a website without Unicode is hard, and your coders know it. Supporting Unicode is possible until a certain point, even in case the used language is not compatible with Unicode, but errors are easy to get. It’s not an easy task. 

Accurate support for Unicode demands a language that can understand Unicode strings. And as we mentioned before, PHP 6 ambitious plan was to support it completely (API, engine, and extensions).

To achieve the goal and make compatibility possible with the International Components for Unicode (ICU) library, using the two-byte UTF-16 encoding for all the strings inside PHP was decided. It sounded logical since ICU’s system used it, and interactions inside ICU were calculated to be more than with outside players like disk files or MySQL. 

Rephrasing, for coders, reading or writing a string from an outside source usually happens once. But many Unicode actions can be required on the same string. A conversion for every action multiplies the job. So to use the two-byte UTF-16 encoding looked like a proper solution, at least in theory.

Unfortunately, the decision was not as useful as expected in practice, and its cost went really high. Still, other conversions were needed to do, for instance, script code, request data, database results, etc., from UTF-8 to the UTF-16. And such conversion demanded more CPU processing. A UTF-8 string required half the memory that UTF-16 took. Besides, due to the conversion, sometimes manual checks were needed to identify the exact location of an error. It could be either in the original encoding or in the converted one.

What happened with PHP 6?

Unicode support became the PHP 6 Achilles heel. No matter the effort, opinions about it were negative. Its performance was qualified as poor, broken scripts were constant, and memory usage went high. 

In March 2010, the development of PHP 6 officially stopped. Its features not related to the Unicode were transferred to the new PHP 5.4. 

bookmark_borderPHP 7 explained

PHP 7 explained

PHP 7 is a significant release of PHP programming language. It is promoted as a revolution. As for how web applications are possible to be developed and produced for the cloud and mobile to enterprises. This particular release is pointed to be the most significant improvement for PHP after the announcement of PHP 5 in 2004. 

Initiation of the development of PHP 7

The initiative for the development of PHP 7 came from Dmitry Stogov, Xinchen Hui, and Nikita Popov. These three developers designed a test branch of PHP that they first named PHP Next Generation (PHPNG). The PHP community embraced it, as it had crucial performance improvements. It continued to develop into a well-built version now known as PHP 7. After some months of beta testing, the official release was in December 2015.

What Drove the Need for PHP 7?

The PHP language is extremely popular. Rasmus Lerdorf created it in 1994. Since then, it has gained popularity, and now approximately 82 percent of websites implement PHP. That indicates that most of the Web is relying on this language. In addition, the amount of people online is continuing to increase. This includes the rapidly growing number of mobile users. Therefore, the servers that power the world’s websites are necessary to be able to respond fast to user requests. Speed is an essential factor in the design of a website. People are going to leave a web page that takes a lot of time to load. 

The PHP changes boosted the performance of sites dramatically. It is estimated that version 7 gives a 100 percent increase in performance speed compared to version 5.6. This allows the developers to make websites that present attractive and interesting interactive traits that can also respond to visitors’ input fast. 

The development of this version was also motivated by the need for scripting languages that work more efficiently. The desire for that is inspired by two things: the need to decrease power consumption to protect the ecosystem and the need to reduce expenses.

It reduces the demands on the servers, which makes it more friendly for the environment and more cost-effective decision. In addition, compared to PHP 5.6, running PHP 7 applications needs less energy to power servers.

New Features

To PHP 7 are added a lot of new features. The most significant ones from them are the following: 

  • Improved performance − It is twice faster than PHP 5 when you have a PHPNG code united in PHP 7.
  • Scalar type declarations − Now, return types and parameters are able to be implemented.
  • Less Memory Usage − The optimized PHP 7 consumes a lot lesser resources.
  • Exception hierarchy − The feature is improved.
  • 64-bit support − 64-bit architecture machines receive constant support.
  • Several fatal errors converted to Exceptions − The variety of exceptions is extended. Including many fatal errors turned into exceptions.
  • Null coalescing operator (??) − A new feature is applied to replace the ternary operation in conjunction with the isset() function.
  • Secure random number generator − Increase of the latest secure random number generator API.
  • Return and Scalar Type Declarations − Added support for the return type and parameter type.
  • Deprecated SAPIs and extensions removed − Several past and unsupported SAPIs and extensions are eliminated from the PHP 7 version.
  • Anonymous Classes − It is added support for anonymous.
  • Zero cost asserts − Also added support for zero cost asserts.

PHP 7 applies new Zend Engine 3.0. The purpose is for the performance of the application to improve twice. Also, the consumption of memory to be 50% better than PHP 5.6. This allows without any extra hardware to work for more concurrent users.

PHP 7 is created contemplating today’s workloads.