PHP8.0新函数

作者: 温新

分类: 【PHP技术】

阅读: 2027

时间: 2022-01-26 12:21:16

**str_contains()**检查另一个字符串是否包含一个指定字符串

返回值:bool

$str = 'this is my blog';
$res = str_contains($str, 'my');
var_dump($res); // true

**str_starts_with()**检查一个字符串是否以指定字符串开头

$str = 'this is my blog';
$res = str_starts_with(haystack:$str, needle:'this');
var_dump($res); // true

**str_ends_with()**检查一个字符串是否以指定字符串结尾

$str = 'images/2022/a.jpg';
$res = str_ends_with(haystack:$str, needle:'.jpg');
var_dump($res); // true

**fdiv()**Divides two numbers, according to IEEE 754

fdiv函数允许除以0,得到浮点型-INFINF的结果,而不是报错。

1)报错的案例

intdiv(1, 0);
// Fatal error: Uncaught DivisionByZeroError: Division by zero in

2)fdiv函数

var_dump(fdiv(1, 0)); // float(INF) 
var_dump(fdiv(-1, 0)); // float(-INF) 

**get_resource_id()**返回给定资源的整数标识符

$handle = fopen('./storage/logs/lumen.log', 'rt');

echo (int) $handle . "\n\n";
echo get_resource_id($handle);

**get_debug_type()**以合适的方式获取变量类型

php8.0以后的版本更推荐使用get_debug_type

1)标量类型

变量类型 案例值 gettype() get_debug_type()
String 'Foo' string string
Arrays [1,2] array array
Null null NULL null
Integers 123 integer int
Float 3.141 double float
Boolean true/flase boolean bool

2)类对象或匿名函数

类型 案例 get_type() get_debug_type()
Class object new StdClass() object stdClass
Class object new DateTime() object DateTime
Class object new Foo\Bar(0) object Foo\Bar
Closure function() {} object Closure
Anonymous class new class {} object class@anonymous
Anonymous subclass new class extends Foo{} object Foo@anonymous

3)资源

类型 案例 gettype() get_debug_type()
Streams tmpfile() resource resource(stream)
Curl handler curl_init() resource resource (curl)
Closed Curl handler curl_close($ch) resource (closed) resource (closed)
XML document xml_parser_create() resource resource(xml)

https://php.watch/versions/8.0/get_debug_type

我是温新

每天进步一点,就一点点

请登录后再评论