Home >Backend Development >PHP7 >Introduce some features of PHP7
Related learning recommendations: php programming (video)
1.Usage of use
<?php // PHP 7 之前版本用法 use some\namespace\ClassA; use some\namespace\ClassB; use some\namespace\ClassC as C; use function some\namespace\fn_a; use function some\namespace\fn_b; use function some\namespace\fn_c; use const some\namespace\ConstA; use const some\namespace\ConstB; use const some\namespace\ConstC; // PHP 7+ 用法 use some\namespace\{ClassA, ClassB, ClassC as C}; use function some\namespace\{fn_a, fn_b, fn_c}; use const some\namespace\{ConstA, ConstB, ConstC}; ?>
2. The serial number function unserialize has a new filtering function
// 转换对象为 __PHP_Incomplete_Class 对象 $data = unserialize($foo, ["allowed_classes" => false]); // 转换对象为 __PHP_Incomplete_Class 对象,除了 MyClass 和 MyClass2 $data = unserialize($foo, ["allowed_classes" => ["MyClass", "MyClass2"]); // 默认接受所有类 $data = unserialize($foo, ["allowed_classes" => true]);
3. Define a constant array through define
<?php define('ANIMALS', [ 'dog', 'cat', 'bird' ]); echo ANIMALS[1]; // 输出 "cat" define("GREETING","Hello you! How are you today?",TRUE); echo constant("greeting"); //返回常量的值 ?>
4. Ternary operator null merge
<?php // 如果 $_GET['user'] 不存在返回 'nobody',否则返回 $_GET['user'] 的值 $username = $_GET['user'] ?? 'nobody'; // 类似的三元运算符 $username = isset($_GET['user']) ? $_GET['user'] : 'nobody'; ?>
If you want to learn more about programming, please pay attention to the php training column!
The above is the detailed content of Introduce some features of PHP7. For more information, please follow other related articles on the PHP Chinese website!