Jom lihat kelebihan dan kenapa kita perlu beralih ke PHP 7.

Seperti yang semua sedia maklum melalui portal, rasmi php.net telah menghentikan pembangunan PHP 5 (versi terakhir sehingga artikel ini ditulis adalah 5.6.30) dan mula fokus kepada pembangunan PHP 7:

The PHP development team announces the immediate availability of PHP 5.6.30. This is a security release. Several security bugs were fixed in this release. All PHP 5.6 users are encouraged to upgrade to this version.

According to our release calendar, this PHP 5.6 version is the last planned release that contains regular bugfixes. All the consequent releases will contain only security-relevant fixes, for the term of two years. PHP 5.6 users that need further bugfixes are encouraged to upgrade to PHP 7.

Jadi sudah masanya untuk kita turut mengemaskini server dan aplikasi agar selari versi terkini ini. Untuk itu jom kita lihat beberapa features baru yang terdapat dalam PHP 7 ini.

Null coalescing operator

The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

<?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
?>

Spaceship operator

The spaceship operator is used for comparing two expressions. It returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b. Comparisons are performed according to PHP’s usual type comparison rules.

<?php
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1

// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
?>

Constant arrays using define()

Array constants can now be defined with define(). In PHP 5.6, they could only be defined with const.

<?php
define('ANIMALS', [
'dog',
'cat',
'bird'
]);

echo ANIMALS[1]; // outputs "cat"
?>

Session options

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

These options have also been expanded to support session.lazy_write, which is on by default and causes PHP to only overwrite any session file if the session data has changed, and read_and_close, which is an option that can only be passed to session_start() to indicate that the session data should be read and then the session should immediately be closed unchanged.

For example, to set session.cache_limiter to private and immediately close the session after reading it:

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

random_bytes & random_int

Generates an arbitrary length string of cryptographic random bytes that are suitable for cryptographic use, such as when generating salts, keys or initialization vectors.

Essentially secure way of generating random data. There are random number generators in PHP, rand() for instance, but none of the options in version 5 are very secure. In PHP 7, they put together a system interface to the operating system’s random number generator. Because we can now use the operating system’s random number generator, if that gets hacked we have bigger problems. It probably means your entire system is compromised and there is a flaw in the operating system itself.

Secure random numbers are especially useful when generating random passwords or password salt.

What does this look like for you as a developer? You now have 2 new functions to use: random_int and random_bytes.

<?php
$bytes = random_bytes(5);
var_dump(bin2hex($bytes)); // outputs string(10) "385e33f741"
?>

Generates cryptographic random integers that are suitable for use where unbiased results are critical, such as when shuffling a deck of cards for a poker game.

Benchmarks for PHP 7 consistently show speeds twice as fast as PHP 5.6 and many times even faster! Although these results are not guaranteed for your project, the benchmarks were tested against major projects, Drupal and WordPress, so these numbers don’t come from abstract performance tests.

<?php
var_dump(random_int(100, 999)); // outputs int(248)
var_dump(random_int(-1000, 0)); // outputs int(-898)
?>

Selain itu antara kelebihan yang ada pada PHP 7 adalah lebih laju daripada PHP 5.6

Benchmarks for PHP 7 consistently show speeds twice as fast as PHP 5.6 and many times even faster! Although these results are not guaranteed for your project, the benchmarks were tested against major projects, Drupal and WordPress, so these numbers don’t come from abstract performance tests.

Untuk rujukan dan maklumat lebih lanjut, sila refer ke pautan berikut:
+ https://github.com/tpunt/PHP7-Reference
+ https://blog.engineyard.com/2015/what-to-expect-php-7
+ https://devzone.zend.com/4693/php-7-glance/
+ https://www.digitalocean.com/company/blog/getting-ready-for-php-7/
+ https://www.colinodell.com/blog/2015-12/five-lesser-known-features-of-php-7
+ https://laracasts.com/series/php7-up-and-running
+ https://www.sitepoint.com/whats-new-and-exciting-in-php-7-1/
+ https://mediatemple.net/community/products/dv/207889153/what’s-new-in-php-7
+ http://www.zend.com/en/resources/php-7

Comments

Published by

PakCu

I'm a simple person