24x7 availability
Hybrid Web Application Development
SEO & Social Media Marketing
Content Creation & Curation
dats_abk
Drop us a message!
+91-6353046688
Call us anytime!
contact@abkmultiservices.com
Drop us your requirement anytime!
Locate Us at
Ahmedabad,IN

Must Know New Features in PHP 7

Home - Technology - PHP - Must Know New Features in PHP 7

Must Know New Features in PHP 7

PHP 7

Php (hypertext processor) is a general purpose scripting language which is focused on server side scripting and can be embedded into HTML. In this article, we will guide you through the recent version of PHP, PHP 7 and its extraordinary features.

What’s new PHP 7?

Type declaration

Type declaration implies the type of variable being set instead of making php set automatically. These declarations help you to define what should occur so that you get the expected results which also enhance your code.

In PHP 7, scalar type definitions is possible either through coersive mode or strict mode. The following types can now be used: Strings, integers, floating points and Boolean.

Output: int(9)

Return type declaration

Return type declaration is similar to scalar type, it specifies the type of the value which is returned from the function. To specify the return type, we add the type and colon before the opening curly bracket.

function getTotal(float $a, float $b) : float {

Although PHP does not require you to declare types, declaring the type makes the code easier to read and forces things to be implemented in the way it is required.

Spaceship operator

PHP 7 also supports new operators; the first one is the spaceship operator. It is a combined operator which complements greater-than and lesser-than operators.

$compare = 3 <=> 4

3 < 4? return -1

3 = 4? return 0

3 > 4? return 1

It is altogether using three individual operators, less-than, equal and greater-than. Essentially, it checks each operator one by one. If the value on the left is less than the value on the right, the operator will yield -1. If not, it will move to the next condition, to check if both the values are equal to each other and return 0. Even if this is not true, it checks the last condition and return 1. It is ideally useful for sorting algorithms.

Null coalescing operator

The next new operator, null coalescing operator will ideally tell you if the value is set or not. It will return the left operand if its not NULL, otherwise it will return the right.

$name = $firstName ??  “Anyone”;

In this example, the null coalescing operator (??) will return the value if it is set for the first name, otherwise will return the string “Anyone”.

Before PHP 7, this was written as,

if (!empty($firstName)) $name = $firstName;else $name = “Anyone”;

Constant Arrays

In PHP7, Constant arrays can now be defined with a method called define(). Previously, it could be only defined using const.

CSPRNG

Cryptographically Secure Pseudo Random Number Generator in PHP is an essentially secure way of generating random numbers. Previously, these operators did not prove to be very secure.

In PHP7, the system’s interface is binded to the operating system’s interface and will cause problems if it gets hacked. These are very useful especially when generating random passwords. PHP7 gives two new functions for the developers to use: random_int and random_bytes

Random Bytes

While using random bytes, you only need to provide the length of the random string you want to generate. Random bytes will then return the cryprographically secured random bytes.

$bytes = random_bytes(5); // length in bytesvar_dump(bin2hex($bytes));// output similar to: string(10) “385e33f741”

Random Int

In random bytes, the output generated was in bytes and not integers. If you want to return a random number, you should use Random int.

While using random int, you need to provide two arguments, min and max which are the minimum and maximum numbers you want to use.

For example,

random_int(1,20);

Other Features

  • Support for anonymous classes has been added by the usage of new class. These can be used for throw away objects.
  • Unicode support for emoji and international characters are supported too!

Conclusion

PHP 7 has a lot of amazing new features which any developer would like. If you’re still using the older version, the huge advantage of speed in PHP 7 would convince you that it’s time for updating your code!

Leave A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Need Urgent Help?

Our team will get in touch soon

Leave a Message