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.

&lt;?php<br />
// Fetches the value of $_GET['user'] and returns 'nobody'<br />
// if it does not exist.<br />
$username = $_GET['user'] ?? 'nobody';<br />
// This is equivalent to:<br />
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';</p>
<p>// Coalescing can be chained: this will return the first<br />
// defined value out of $_GET['user'], $_POST['user'], and<br />
// 'nobody'.<br />
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';<br />
?&gt;

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.

&lt;?php<br />
// Integers<br />
echo 1 &lt;=&gt; 1; // 0<br />
echo 1 &lt;=&gt; 2; // -1<br />
echo 2 &lt;=&gt; 1; // 1</p>
<p>// Floats<br />
echo 1.5 &lt;=&gt; 1.5; // 0<br />
echo 1.5 &lt;=&gt; 2.5; // -1<br />
echo 2.5 &lt;=&gt; 1.5; // 1</p>
<p>// Strings<br />
echo &quot;a&quot; &lt;=&gt; &quot;a&quot;; // 0<br />
echo &quot;a&quot; &lt;=&gt; &quot;b&quot;; // -1<br />
echo &quot;b&quot; &lt;=&gt; &quot;a&quot;; // 1<br />
?&gt;

Constant arrays using define()

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

&lt;?php<br />
define('ANIMALS', [<br />
'dog',<br />
'cat',<br />
'bird'<br />
]);</p>
<p>echo ANIMALS[1]; // outputs &quot;cat&quot;<br />
?&gt;

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:

&lt;?php<br />
session_start([<br />
'cache_limiter' =&gt; 'private',<br />
'read_and_close' =&gt; true,<br />
]);<br />
?&gt;

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.

&lt;?php<br />
$bytes = random_bytes(5);<br />
var_dump(bin2hex($bytes)); // outputs string(10) &quot;385e33f741&quot;<br />
?&gt;

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.

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

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