slot deposit pulsa slot mahjong slot gacor slot gacor slot gacor resmi slot gacor 2025 slot gacor terpercaya slot gacor 2025 slot gacor hari ini slot gacor hari ini slot gacor hari ini
2023 年你应该知道的 20 个 PHP 特性
17611538698
webmaster@21cto.com

2023 年你应该知道的 20 个 PHP 特性

编程语言 0 1680 2023-06-05 07:45:57

图片

PHP 一直在发展,保持与最新功能和改进保持同步非常重要。本文介绍了截至 2023 年您应该了解的 20 项 PHP 功能,每个功能都配有一个方便的代码示例。


1.str_contains():

检查一个字符串是否包含在另一个字符串中。

$sentence = "The quick brown 🦊 jumps over the lazy 🐶." ; 
$word = "🦊" ;
if ( str_contains ( $sentence , $word )) {
echo "这个句子包含🦊。" ;
}

2. str_starts_with():

检查字符串是否以给定的字符串开头。

$sentence = "🚀 发射到太空!" ; 
if ( str_starts_with ( $sentence , "🚀" )) {
echo "句子以火箭表情符号开头!" ;
}

3. str_ends_with():

检查字符串是否以给定的子字符串结尾。

$sentence = "这真的是美好的一天!☀️" ; 
if ( str_ends_with ( $sentence , "☀️" )) {
echo "句子以太阳表情符号结尾!" ;
}

4. get_debug_type():

获取变量的类型。

$num= 42 ; 
echo get_debug_type ( $num ); // “整数”

5. get_resource_id():

返回给定资源的唯一标识符,或称句柄ID。

$file = fopen ( 'test.txt' , 'r' ); 
echo get_resource_id ( $file ); // 例如,“7”

6. fdiv():

新的除法函数,包括对除以零的支持。

$result = fdiv ( 10 , 0 );

7. preg_last_error_msg():

返回上次 PCRE 正则表达式执行错误,为人类可读消息。

preg_match ( '/(/' , '' ); 
echo preg_last_error_msg (); // 返回 “missing )”

8. array_key_first():

获取数组的第一个键(Key)。

$array = [ '🍏' => '苹果' , '🍊' => '橙子' , '🍇' => '葡萄' ]; 
echo array_key_first ( $array ); // "🍏"

9. array_key_last():

有第一个就有最后一个,array_key_last()为获取数组的最后一个键。

$array = [ '🍏' => '苹果' , '🍊' => '橙子' , '🍇' => '葡萄' ]; 
echo array_key_last ( $array ); // "🍇"

10. 错误异常::getSeverity():

获取错误的严重性。

try { 
trigger_error ( "自定义错误" , E_USER_WARNING);
} catch ( ErrorException $e ) {
echo $e -> getSeverity (); // 512
}

11.过滤器函数:

PHP 8 引入了几个新的过滤器函数。下面是一个使用filter_varwith的例子FILTER_VALIDATE_BOOL

var_dump ( filter_var ( 'yes' , FILTER_VALIDATE_BOOL)); // 布尔值(真)

12.Weak Map:

一个包含对象引用的新类,它不会阻止这些对象被垃圾回收。

$weakmap = new  WeakMap (); 
$obj =new stdClass();
$weakmap [ $obj ] = 'Hello, world!' ;

13. 值对象:

PHP 8 引入了 Constructor Property Promotion,一种用于构造值对象的新语法。

class  Money  {
public function __construct (
public int $amount ,
public string $currency
)
{}
}

$tenDollars = new Money ( 10 , 'USD' );

14.匹配表达式:

这是一个类似于Switch的语句。

echo match ( 1 ) { 
0 => '🚫' ,
1 => '✅' ,
default => '⁉️' ,
};

15. Nullsafe 运算符:

这个新运算符 (?->) 允许在访问属性或方法时,进行空值检查。

class User {

    public function getAddress(): ?Address {

        // returns Address or null

    }

}

$user = new User();

$country = $user?->getAddress()?->country; // no error if getAddress() returns null

16.命名参数:

此功能允许开发者通过指定值的名称,将值传递给函数。

new Money(amount: 10, currency: 'USD');

17.属性:

在其它编程语言中也称为注解。

#[Attribute ]
ExampleAttribute {}

#[ExampleAttribute ]
ExampleClass {}

18.构造方法推介:

此功能允许将类属性和构造函数组合到同一个声明中。

class Money {

    public function __construct(

        public int $amount,

        public string $currency

    ) {}

}

19.联合类型:

此功能允许可以是多种类型之一的类型声明。

function  print_id ( int | string  $id ): void  {
echo 'ID: ' . $id ;
}

20. 即时编译(JIT):

PHP 8 引入了两个 JIT 编译引擎,Tracing JIT 和 Function JIT。

注意:JIT 编译不是可以直接用代码片段演示的功能,但它是 PHP 8 中的一项重要改进,可以提供非常明显的性能改进。


总之,PHP 是一种不断发展的语言,具有许多令人兴奋的新特性和改进。

无论你是经验丰富的 PHP 开发人员还是新手,都值得花时间了解这些新功能,并在自己的代码中使用它们。

保持好奇,不断学习,快乐编程!🚀

评论