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


$ 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) {


  string(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





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

$ chmod +x test

$ ./test -h — foo

array(4) {


  string(6) “./test”


  string(2) “-h”


  string(2) “–“


  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:



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


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


   <?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.


 } 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: 


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. 


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 


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” 

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

rpm – Uvh

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. 


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.


  • 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.