当前位置:网站首页>Traps in the explode function in PHP

Traps in the explode function in PHP

2022-07-05 20:56:00 Fengshen Magic Dragon

In general , All are carried out when the parameter is not empty explode operation , for example

$a = "a,b,c";
$b = explode(',',$a);

The result will be returned

array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" }

But what if the passed in parameter is empty ?

$a = '';
$b = explode(',',$a);

What results will be returned at this time , Will you think that return

array(0) { }

It's not , It will be returned

array(1) { [0]=> string(0) "" }

If you treat it as a short array , Then all the subsequent logic will go wrong , Because it is not an empty array ,empty It does not return true, So in use explode Be sure to pay attention to the value of the second parameter , It's best to make a non empty judgment first , Later explode operation .

原网站

版权声明
本文为[Fengshen Magic Dragon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207052049275756.html