Change language

What’s new in PHP 7?

There were many minor releases in PHP 5 that were interesting along the way, including the provision of object-oriented programming and many of the features associated with it.So why 7 and not 6 ?
Almost all of the features discussed for PHP 6 were eventually executed in PHP 5.3 and executed successfully, so nothing was overlooked. Eventually, new tools were added for feature requests. When the flag set for a significant release was adopted to avoid confusion, it moved to version 7 for the newest release.What makes PHP 7 unique?
Speed ​​and performance got better thanks to PHP 7 - less memory used PHP codebase - a faster engine, and as they say, let’s see what has been added to this package
  • Arrays of constants using define():a PHP array constant can be defined using define() function. In PHP 5.6, this is defined by the const keyword.  
    // Use the define() function define ( ’ GFG’ , [ ’Geeks’ , ’ G4G’ , ’geek’ ] );  
    // Show content echo GFG [0]; ?>
    Exit:
    Geeks 
  • Unicode Escape:PHP 7.0.0 introduced the "Unicode escape point". It uses the Unicode code point in hexadecimal and outputs that code point in UTF-8 into a double quoted string. Any valid code point is permitted, including leading 0s as arbitrary. Thus, you can efficiently write Unicode characters using double quotes or the heredoc string without calling a function.  
    // Write PHP code echo "u {aa}" ; echo "u {0000aa}" ; echo "u {9999}" ; ?>
    Exit:
    ªª�
    It is possible to escape from symbols earlier, but with PHP 7 it is much easier. Including Unicode in a string without the hassle.  
    // Write PHP code echo ’ You owe me £ 500.’ ; ?>
    Exit:
    You owe me £ 500.
    This example uses the standard characters A through Z and 0 through 9 characters. But it contains the special character pound (£). Previously, it was necessary to escape these characters, which would otherwise produce a 163 character code per string. So the result might look like 163500 or something. This would be a nightmare, so we have to avoid the pound symbol every time, which would otherwise cause an error. 
    So how do you use Unicode escaping? Unicode practice hexadecimal, to determine the number, take a look at the result // Write PHP code echo ’ You owe me u {A3} 500.’ ; This will also display correctly:You owe me £ 500.So the backslash u and curly braces (/ u {}) are the basic syntax for inserting a Unicode hexadecimal character into a string.
  • Filtered Unserialize():This function is used to provide more security when deserializing objects on untrusted data. This prevents code injection. Security is a top priority for us, which better ensures that objects are not serialized to data from an unknown source, or is unreliable, which allows the developer to create whitelisted classes that may not be serialized. It takes one serialized variable and returns a PHP value. < tbody>  
    // Write PHP code class A { public $obj1var ; } class B { public $obj2var ; } $obj1 = new A(); $obj1 -> obj1var = 10; $obj2 = new B(); $obj2 -> obj2var = 20; $serializedOb1 = serialize ( $obj1 ); $serializedOb2 = serialize ( $obj2 );  
    // If allowed_classes are passed as
    // false, unserializing transformations
    // every all objects in
    // __PHP_Incomplete_Class object $data = unserialize ( $serializedOb1 , [ "allowed_classes" = > true]);  
    // Converts all objects to
    // __PHP_Incomplete_Class object
    // except those from A and B $data2 = unserialize ( $serializedOb2 , [ "allowed_classes" = > [ "A" , " B " ]]); print ( $data -> obj1var); print ( "
    "
    ); print ( $data2 -> obj2var);  
    ?>
    Exit:
    10 
    20
  • Group Usage Declaration:PHP 7 has implemented the group concept in the PHP namespace. This is one of the great additions to namespaces in PHP 7. They are simpler and make it easier to import classes, constants, and functions in a compact way. Bulk declarations provide the ability to import multiple structures from a standard namespace and, in most cases, reduce the verbosity level. Bulk declarations make it easier to identify the various imported objects that belong to an equivalent module.
  • Expectations.We all have certain assumptions, but not all of those assumptions are facts. Assumptions are based on logic and guesswork, but this does not treat them as facts. The assumptions may sound logical, but they must be backed up by code to protect the integrity of the processing. Expectations mean the enhancement is backward compatible with the older assert() function. It provides the ability to create custom exceptions if the assertion is default. assert() first parameter - it is an expression compared to a string or boolean to be examined.  
    // Write PHP code $num = 300; echo assert ( $num > 600, new CustomError ( "Assumed num was greater than 600 " ));  
    ?>
    Exit:
    1 
  • Session Options:PHP 7 introduces session_start() to allow an array of options that override configuration directives often set in php.ini. 
    For example, the session.cache_expire directive is 180, but let’s say I want my cache to expire for a specific session. // Write PHP code
    session_start ([ cache_expire = > 380 ]);
    Now there is no need to change this option by reconfiguring the server settings.

  • Generator Return Expressions:PHP 5.5 introduced a generator function that included yield values, and the return statement had to be the last one in it. // Write PHP code function genA() { yield 20; yield 30; yield 40; return 50; } According to the PHP manual, “a generator function looks like a regular function, except that instead of returning a value, the generator returns as many values ​​as needed. Any function that contains yield is a generator function. It can iterate over and retrieve values, but if someone tries to return anything from it, an error will be thrown.

    $genObj = function ( array $number ) { foreach ( $number as $num ) { yield $num * $num ; } return " Done calculating the square. " ; }; $result = $genObj ([10, 20, 30, 40, 50]); foreach ( $result as $value ) { echo $value . PHP_EOL; }  
    // Get the return value echo $result -> getReturn();  
    ?>
    Exit:
    100 400 900 1600 2500 Done calculating the square.
  • Generator delegation.In PHP 7, generator delegation - it is the value of the exit from another generator, an object that can be traversed, or an array using the yield keyword from the keyword. The external generator will then emit all values ​​from the internal generator, objects, or array until they become invalid, after which execution will continue in the external generator. If a generator is used with yield from, the yield from expression will also return any value returned by the inner generator.  
    // Write PHP code function mul ( array $number ) { foreach ( $number as $num ) { yield $num * $num ; } yield from sub ( $number ); }; function sub ( array $number ) {

    foreach ( $number as $num ) { yield $num - $num ; } } foreach (mul ([1, 2, 3, 4, 5]) as $value ) { echo $value . PHP_EOL; }  
    ?>
    Exit:
    1 4 9 16 25 0 0 0 0 0
Link: http://php.net/manual/en/migration70.new-features.php

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method