Why did the null return 1 element explode?

var_dump(count(explode(',',[]))); // 0
var_dump(count(explode(',',''))); // 1
var_dump(count(explode(',',null))); // 1 ????

I was expecting 0 for the latter. Can someone tell me the cause of the explosion with a nullvalue of 1, not 0? (PHP 5.6.16)

+4
source share
1 answer

explodetakes a specification string as the second argument . You gave it null, so before starting the logic explodePHP converts nullto a string. Each function works as follows: if the arguments do not correspond to the formal specification, PHP tries to “type juggling” until this happens. If PHP can juggle, the engine moves together happily, otherwise you will get a faint slap on the wrist:

PHP Warning: explode() , 2 , .php 1

PHP : null := ''. , null '' - , :

$a = explode(',', '');
$b = explode(',', null);
var_dump($a === $b); // true

$a = count(explode(',', ''));
$b = count(explode(',', null));
var_dump($a === $b); // true

, : PHP ? :

, ... , .

+7

All Articles