Analyze the difference between php7 and PHP5 in detail

Time:2022-5-18

1. PHP scalar type and return type declarations

#There are two main modes: mandatory mode and strict mode
declare(strict_types=1)
#1 indicates strict type verification mode, which is used for function calls and return statements; 0 indicates weak type verification mode.

2. Null merge operator

$site = isset($_GET['site']) ? $_GET['site'] : 'wo';
#Abbreviated as
$site = $_GET['site'] ??'wo';

3. Combination budget symbol

//Integer comparison
print( 1 <=> 1);print(PHP_EOL);
print( 1 <=> 2);print(PHP_EOL);
print( 2 <=> 1);print(PHP_EOL);
print(PHP_EOL); //  PHP_ EOL is a newline character
//Results:
0
-1
1

4. Constant array

//Use the define function to define an array
define('sites', [
   'Google',
   'Jser',
   'Taobao'
]);

print(sites[1]);

5. Anonymous class

interface Logger { 
   public function log(string $msg); 
} 

class Application { 
   private $logger; 

   public function getLogger(): Logger { 
      return $this->logger; 
   } 

   public function setLogger(Logger $logger) { 
      $this->logger = $logger; 
   }   
} 

$app = new Application; 
//Create an anonymous class using new class 
$app->setLogger(new class implements Logger { 
   public function log(string $msg) { 
      print($msg); 
   } 
}); 

$app - > getlogger() - > log ("my first log");

6. The closure:: call() method is added to bind an anonymous function to the class

<?php 
class A { 
    private $x = 1; 
} 

//Closure function code defined in PHP before version 7 
$getXCB = function() { 
    return $this->x; 
}; 

//The closure function is bound to class A 
$getX = $getXCB->bindTo(new A, 'A');  

echo $getX(); 
print(PHP_EOL); 

//PHP 7 + code 
$getX = function() { 
    return $this->x; 
}; 
echo $getX->call(new A); 
?>

7. Csprng (pseudo random number generator).

PHP 7 provides a simple mechanism to generate cryptographically strong random numbers by introducing several csprng functions.

random_ Bytes () – encrypt the protected pseudo-random string.

random_ Int () – a pseudo-random integer protected by encryption.

8. Abnormal

PHP 7 exceptions are used for downward compatibility and enhancement of the old assert() function.

9. Use statement change

#Class abbreviations under the same namespace can be imported
use some\namespace\{ClassA, ClassB, ClassC as C};

10. Session options

//1. session_ Start() can define an array
<?php
session_start([
   'cache_limiter' => 'private',
   'read_and_close' => true,
]);
?>
//2. Introduced a new PHP Ini setting (session. Lazy_write), which is set to true by default, means that session data is written only when it changes.

11. PHP 7 removed extensions

  • ereg
  • mssql
  • mysql
  • sybase_ct

Why is php7 better than PHP5?

1. Variable storage bytes are reduced to reduce memory consumption and improve variable operation speed

2. The array structure is improved. The array elements and hash mapping table are allocated in the same memory, which reduces the memory occupation and improves the CPU cache hit rate

3. The function calling mechanism is improved. By optimizing the link of parameter transmission, some instructions are reduced and the execution efficiency is improved

This is the end of this article about the detailed analysis of the differences between php7 and PHP5. For more information about the differences between php7 and PHP5, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

Recommended Today

Modul of fastems

Each module of fastems is implemented from the abstract class Fastems.Mms.Client.Infrastructure.UiModuleBase; public class DataManagerModule : UiModuleBase { public override void Initialize() { AddResourceDictionary(“/Resources/DataManagerResources.xaml”, typeof(DataManagerModule)); RegisterViewWithRegion(“DialogRegion”, typeof(DialogView)); RegisterViewWithRegion(“BusyIndicatorRegion”, typeof(BusyIndicatorView)); } } And Fastems.Mms.Client.Infrastructure.UiModuleBase inherits from Fastems.Mms.Client.Infrastructure.ModuleBase public abstract class UiModuleBase : ModuleBase { [Import] public IRegionManager RegionManager { get; set; } [Import] public IMergedDictionaryRegistry MergedDictionaryRegistry { […]