Bug #55772 [Opn->Bgs]: is_array throws E_NOTICE on undefined array elements, isset does not.
| From: | aharvey@php.net | Date: | Sat, 24 Sep 2011 19:06:36 +0000 |
| Subject: | Bug #55772 [Opn->Bgs]: is_array throws E_NOTICE on undefined array elements, isset does not. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-163838@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=55772&edit=1
ID: 55772
Updated by: aharvey@php.net
Reported by: rick dot foos at solengtech dot com
Summary: is_array throws E_NOTICE on undefined array
elements, isset does not.
-Status: Open
+Status: Bogus
Type: Bug
Package: Scripting Engine problem
Operating System: Fedora/Ubuntu
PHP Version: 5.3.8
Block user comment: N
Private report: N
New Comment:
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
is_array() tests the type of the given variable. It doesn't test
whether the variable itself exists -- it assumes it does, and checking
that is the job of isset().
Also, anonymous, should you check back in: perhaps you could avoid
describing reports made in good faith as "blithering". And put a name
to your comments, although I can understand why you might want to
disown that one.
Previous Comments:
------------------------------------------------------------------------
[2011-09-24 16:08:31] rick dot foos at solengtech dot com
Sorry for blithering, long day, and most of the comments on arrays involved lectures. isset works,
is_array returns true/false correctly, but throws an E_NOTICE.
is_array is a test, and should not throw an error when the test is false.
------------------------------------------------------------------------
[2011-09-24 15:14:42] anonymous at nowhere dot never
>is_array is the correct call to use.
What are you blithering about? I think you mean isset(). is_array has nothing to do with it.
Anyway, the problem you're having is because you're treating $data_array as a
multidimensional array. It's not. PHP has arrays of arrays, not multidimensional arrays (in
languages like this you could make a triangular array if you wanted). So if the row is not defined
then testing isset on the row's column will complain about the undefined row. So you have to
isset on the row to make sure that's there first before you can test its column elements.
E.g.:
if (isset($data_array[$r + 1]) && isset($data_array[$r + 1][$c + 1])) ...
Or like this:
for ($r = 0; $r < $row_count; $r++) {
echo "<tr>";
if (isset($data_array[$r + 1])) {
$row = $data_array[$r + 1];
for ($c = 0; $c < $col_count; $c++) {
if (isset($row[$c + 1])) {
$data = $row[$c + 1];
} else {
$data = null;
}
echo "<td>", $data;
}
} else {
echo str_repeat("<td>", $col_count);
}
}
------------------------------------------------------------------------
[2011-09-24 00:29:27] rick dot foos at solengtech dot com
Description:
------------
I have a sparse matrix, that needs each element to display on an html page. So when the matrix is
loaded with defined values there are many undefined values, 6158 for example.
This is displayed as a square matrix using an HTML table - A conflict between good PHP programming
and HTML programming. The undefined elements need some html, so the 'for loops' must touch
each element of the array to display the square set of values.
is_array is the correct call to use. It tests array elements, and returns true or false if they are
valid or not.
While the is_array function returns true and false - PHP still issues the warning message. is_array
is a test, not an operation on a null value, and therefore not bad programming requiring a message.
http://rickfoosusa.blogspot.com/2011/09/howto-clear-php-undefined-offset.html
Test script:
---------------
$data_array=array();
$data_array[2][10] = "defined";
$row_count = 10;
$col_count = 10;
//...fill with the defined values...many values undefined.
for ($r = 0; $r < $row_count; $r++)
{
echo "<tr>";
for ($c = 0; $c < $col_count; $c++)
{
if (isset($data_array[$r+1][$c+1])){
$data = $data_array[$r+1][$c+1];
} else {
$data = NULL;
}
echo "<td>";
}
}
Expected result:
----------------
True or False depending if the array element is valid.
Actual result:
--------------
True or False depending if the array element is valid.
PHP Notice: Undefined offset: 118 in /usr/share/resultsdb/www/index.php on line 465, referer: http://134.86.96.216/resultsdb/index.php?
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55772&edit=1