Wednesday, October 27, 2021
More

    New Features in PHP 7

    Must Read

    Programmerhttp://www.improgrammer.net
    We started this site to inspire young minds to motivate and encourage them towards Programming Language. In this site you will get programming tutorials, tech, programming facts, programming fun and programming blogs.

    So what makes PHP 7 so special? What does this mean for you as a developer?

    It will include the following Feature.

    • Speed!
    • Type Declarations
    • Error Handling
    • New Operators
    • Easy User-land CSPRNG
    • Conclusion

    1. Speed!

    The developers treated very hard to refactor the PHP codebase in order to decrease memory consumption and increase performance. And they surely succeeded. Benchmarks for PHP 7 always show speeds twice as speed up as PHP 5.6 and many eras even faster! Although these results are not guaranteed for your deal the benchmarks were tested.

    Speed
    Speed

    And they certainly gained. Benchmarks for PHP 7 frequently show speeds twice as fast as PHP 5.6 and many times even faster! even if these results are not secured for your project, the benchmarks were tested on major projects, Drupal, and WordPress, so these numbers don’t come from abstract performance tests.

    And they certainly succeeded. Benchmarks for PHP 7 consistently show speeds twice as fast as PHP 5.6 and many times even faster! Even if these results are not secured for your project, the benchmarks were proved against major projects, Drupal, and WordPress, so these numbers don’t come from abstract conduct performance tests.

    2. Type Declarations

    Type declarations can help you explain what should arise so that you get the normal results. This can also make your code obvious to read. We’ll look at some clear-cut examples shortly.

    Since PHP 5, you can use type hinting to specify the normal data type of an argument in a function report but only in the declaration. When you call the function, PHP will audit whether or not the arguments are of the stated type. If not, the runtime will hike an error and hanging will be halted. Besides only life used in function

    If not, the runtime will raise an error and implement will be halted. Besides only being used in function declarations, we were also limited to at heart 2 types. A class name or an array.

    Here’s an example:

    If we were to create a function for enrolling apprentice we could lack that the first argument is an object of the student class and the second argument to be an array of classes. If we tried to pass just the name rather than an object we would get a fatal error. If we were to pass a distinct class instead of an array, we would also get an error. We are vital to pass a student object and an array.

    If we were to try to check for a scalar variable such as a string, PHP 5 expects it to be an object of the class string, not the variable type string. This means you’ll get a Fatal error: Argument 1 passed to stringTest() must be a detail of string, string given.

    Scalar Type Hints

    With PHP 7 we now have added Scalar types.  Specifically: int, float, string, and bool. By adding scalar type hints and enabling strict claim, it is count on that more correct and self-log PHP programs can be written. It also gives you more control over your code and can make the code clear to read.

    By default, scalar type-avowal are non-strict, which means they will attempt to change the original type to match the type stated by the type-declaration.

    Return Type Declarations

    PHP 7 also supports Return Type expression which helps, all the same, types of arguments. To specify the return type, we include a colon and then the type right ahead the opening curly bracket.

     If we specify the return type of float, it will work quite like it has been in the previous 2 examples since the type being returned was once afloat. Adding the return type allows you to to be sure your function returns what is wonted as well as preparing it easy to see upfront how the function works.

    Non-strict int

    If we specify the return type as int outside strict types set, All will work the same as it did without a return type, the only dissimilarity is that it will force the return to be an int. In the third call, the return value will truncate to 3 because the floating point will be discontinued.

    Strict int

    If we turn strict types on, we’ll get a Fatal error: Uncaught TypeError: Return value of getTotal() should be of the type integer, float returned. In this case, we’ll need to specifically cast our return value as an int. This will then return the shortened value.

    3. Error Handling

    In PHP 7, an exception will be launched when a fatal and reparable error occurs, rather than just stopping the script. Fatal errors still exist for certain conditions, such as running out of memory, and still behave as before by away stopping the script. An uncaught exception will also continue to be a fatal error in PHP 7. This means if an omission thrown from an error that was fatal in PHP 5 goes uncaught, it will still be a fatal error in PHP 7. I want to point out that other types of errors such as warnings and notices remain stable in PHP 7. Only fatal and recoverable errors throw omission.

    In PHP 7, Error and Exception both appliance the new Throwable class. What that means is that they basically work the same way. And also, you can now use Throwable in try/catch blocks to catch both Exception and Error objects. Remember that it is better practice to catch more specific exception classes and knob each accordingly.

    New Hierarchy

    The Throwable interface is implemented by both Exception and Error. Under Error, we now have some more specific error. TypeError, ParseError, A couple arithmetic errors and an AssertionError.

    Throwable Interface

    If Throwable was defined in PHP 7 code, it would look like this

    If you’ve worked with Exceptions at all, this interface should look common Throwable specifies methods nearly identical to those of Exception. The only variation is that Throwable::getPrevious() can return any instance of Throwable alternatively of just an Exception.

    4. New Operators

    Spaceship Operator

    PHP 7 also brings us some new operators. The first one we’re going to try is the spaceship operator. With a name like that, who doesn’t want to use it? The spaceship operator, or Combined Comparison Operator, is a nice addition to the style complementing the greater-than and less-than operators.

    Spaceship Operator  < = >

    First, less than. If the value on the left is less than the value on the right, the spaceship operator will return -1. If not, it will move on to test if the value on the left is EQUAL to the value on the right. If so, it will return 0. If not, it will move on to the final test. If the value on the left is GREATER THAN the value on the right. Which, if the other 2 haven’t passed, this one must be true. And it will return 1.

    Null Coalesce Operator

    Another new operator, the Null Coalesce Operator, is definitely the famous if-set-or. It will return the left operand if it is not NULL, otherwise, it will return the right. The important thing is that it will not raise a notice if the left operand is a non-existent variable.

    5. Easy User-land CSPRNG

    What is Easy User-land CSPRNG?

    User-land refers to a function space that is external to the kernel and is protected by privilege separation, API for an easy to use and positive  Cryptographically Secure PseudoRandom Number Generator in PHP.

    Essentially a secure way of developing random data. There are aimless 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 alloy 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.

    Random Bytes

    When using random_bytes, you stock a single argument, length, which is the length of the random string that should be returned in bytes. random_bytes then returns a string containing the requested number of cryptographically secure random bytes. If we combine this with something like bin2hex, we can get the hexadecimal representation.

    Random Int

    When using random_int, you supply 2 arguments, min, and max. This is the minimum and maximum numbers you want to use.

    Conclusion

    There are quite a few other features added in PHP 7, like Unicode medium for emoji and international characters.

    But this should give you a taste of what’s changing in PHP. The additionally big area that could cause trouble, are features that have been removed. This should easily only be an issue if you’re working with an older code base because the features that have been evacuated are primarily ones that have been deprecated for a long time.

    See more:

    Php Conditional Statements

    Latest Articles

    More Recipes Like This