28
Jun

Understand PHP 7 New Features In Easiest Way

php7-new-features-ficode

In web development; PHP is the most useful language and more than 82% of total world-wide websites are built in PHP, including some major websites like Facebook, Yahoo, Flicker, Tumblr, Baidu, Wikipedia, Digg, Friendster etc. Using PHP for website development brings a lot of advantages. We can connect any type of database with PHP like MYSQL, MONGODB, ORACLE and much more. PHP has its own predefined functions through which you can get instant results. It has a centralized database that helps a lot with coding.

There are many frameworks and CMSs of PHP for different requirements, like for online selling portals “Magento” and “OpenCart” is widely used while ‘WordPress’ is good for blogging and dynamic websites. “CakePHP”, “YII” and “LARAVEL” PHP frameworks are used for complex web applications.

PHP 7 was major release after PHP v5.6, PHP 6 was never released. PHP 7 majorly improved performance, code optimization, removed duplicate, deprecated functions and improved security.

Top 10 PHP 7 New Features Are Listed Below –

 1) Type Declarations: PHP does not need you to declare the data type. It does it automatically & silently. Thus, sometimes you may get unexpected results. To ensure that more correct & well-documented PHP programs can be written and there is more control over your code, “Scalar type declaration” is introduced in PHP 7.

There are two types of Scalar type declarations:

  • Non-strict (Coercive) mode: It is by default mode
  • Strict : This mode can be declared by “declare(strict_types=1);” line in top of file. This MUST be the very first line, even before namespaces.
Example:

<?php
   // Coercive mode
function add(int $a, int $b ) {
return $a + $b;
}
print(add(2, '3.8'));
   // Output will be 5 , because "3.8" will be converted into "int" as 3 automatically
?>

<?php
  // Strict mode
declare(strict_types=1); // This line is compulsary to define strict type
function add(int $a, int $b, ) {
return $a + $b;
   }
print(add(2, '3.8'));
   // It will return "Fatal error" because in strict mode 'add' function need second parameter as "int" but "string" given . 
?>

 

2)  Return Type Declarations: To ensure the type of output from a function,’Return Type Declarations’ feature is added to PHP 7. It will throw “Fatal error: Uncaught TypeError” if data returned by function does not match the type defined in the function definition.

“Type hints” ensure “input consistency” but “return type declarations” ensure “output consistency”.

Example :

function add(int $a, int $b ) : int { }

In non-strict mode, this declaring will be ignored but in strict-mode, this function will throw Fatal error if output is not ‘int’.

 

3) Null Coalescing Operator: It is an optimized version of ‘ternary operator’. It returns its first operand if it exists and is not NULL otherwise second operand.

Example :

 $ternaryOperator=  isset($_GET['variable']) ? $_GET['variable'] : 'not passed';
 $nullCoalescingOperator = $_GET['variable'] ?? 'not passed';

 

4) Combined Comparison Operator (Spaceship Operator) : It compares 2 expressions and return -1 if first is less then 2nd , 0 if both are equal and 1 if second is less than 1st expression. It can be used on any two operands, not just strings, but also integers, floats, arrays, etc.

Example :

print( 1 <=> 1); // Output : 0 
print( 1 <=> 2); //  Output : -1
print( 2 <=> 1);  // Output : 1 

 

5) Constant Arrays: Now arrays also can be defined as constants.

Example:

<?php
define('colors', array('red','blue','green'));
?>

 

6) Anonymous Classes: Anonymous class can be used in place of a full class definition.

Example :

<?php
                $util->setLogger(new class {
                public function log($msg)
                    {
                echo $msg;
                    }
                });
?>

 

7) CSPRNG : Following 2 functions are introduced to generate cryptographically secure integers and strings.

  • random_bytes() : Generates cryptographically secure pseudo-random bytes
Example :

                <?php
                                $bytes = random_bytes(5);
                                var_dump(bin2hex($bytes));
                ?>
  • random_int() : Generates cryptographically secure pseudo-random integers
Example :

                <?php
                                var_dump(random_int(100, 999)); // Output : int(480)
                ?>

 

8) Single ‘Use’ Statement : A single use statement can be used to import Classes, functions and constants from same namespace instead of multiple use statements.

Example :

<?php
use com\phpcode\{ClassA, ClassB, ClassC as C};
use function com\phpcode\{fn_a, fn_b, fn_c};
useconst com\phpcode\{ConstA, ConstB, ConstC};
?>

 

9) New “intdiv()” function: It performs an integer division of its operands and returns it.

Example :

<?php
                var_dump(intdiv(20, 6));
                // Output :int(3)
?>

 

10) Session options: session_start() now accepts an array of options that override the session configuration directives normally set in php.ini.

Example :

<?php
                session_start([
                'cache_limiter' =>'private',
                'read_and_close' => true,
                ]);
?>

Above we discussed that how PHP can be helpful in building faster and better website. It helps fix the most technical vulnerabilities. If you like this article you may also like – How to use MSSQL Server with Cake PHP. Contact us today to upgrade PHP in your website or integrate PHP 7.0 in your application.

share

Judy Malware on Android: 36.5 Million Android Users Affected Globally

Judy Malware on Android: 36.5 Million Android Users Affected Globally

previous-blog-arrowPrevious
How to work with Ingresso API for Theatres Data

How to work with Ingresso API for Theatres Data

next-blog-arrowNext

Memberships / Affiliations