php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80902 is_null is not detecting nulls in array created via explode
Submitted: 2021-03-24 11:16 UTC Modified: 2021-03-24 11:18 UTC
From: floriparob at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: Strings related
PHP Version: 7.4.16 OS: Debian 11
Private report: No CVE-ID: None
 [2021-03-24 11:16 UTC] floriparob at gmail dot com
Description:
------------
Version: 7.4.15-5+deb11u1

When you explode a string into an array, elements that contain nothing (null) are detectable as such via is_null.
The only way to tell is any array element doesn't contain a value is to
strlen(trim($a_array[2])) == 0, which gives you the result you want, but is a tad tedious. 
I'm sure in prior versions is_null($a_array[2] would return true when that array element is empty.
I cannot prove or disprove this as I don't have access to an older version of PHP.

Test script:
---------------
<?php

$a_string = "awsed|34r5|||gh||";
$a_array  = explode ("|", $a_string);
if (is_array($a_array)) {
	$a_count = count($a_array);
	echo "Number of Elements " . $a_count . PHP_EOL;
	for ($i = 0; $i < $a_count; $i++) {
		if (is_null($a_array[$i])) {
			echo "Element " . $i . " Is NULL" . PHP_EOL;
		} else {
			echo "Element " . $i . " is " . $a_array[$i] . PHP_EOL;
		}
	}
} else {
	echo "NOT AN ARRAY" . PHP_EOL;
}
?>




Expected result:
----------------
Number of Elements 7
Element 0 is awsed
Element 1 is 34r5
Element 2 Is NULL 
Element 3 Is NULL
Element 4 is gh
Element 5 Is NULL
Element 6 Is NULL

Actual result:
--------------
Number of Elements 7
Element 0 is awsed
Element 1 is 34r5
Element 2 is 
Element 3 is 
Element 4 is gh
Element 5 is 
Element 6 is

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-03-24 11:18 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2021-03-24 11:18 UTC] cmb@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

See also <https://3v4l.org/jIB0J>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 07 21:01:27 2024 UTC