You are here: Indexing non-arrays as arrays, no warnings? « PHP Programming Language « IT news, forums, messages
Indexing non-arrays as arrays, no warnings?

Posted by ljb on 04/24/07 00:49

I was a bit surprised to find that indexing some non-arrays as if they were
arrays does not even raise a Notice message. This works for NULL, integer,
and floats at least. (PHP-5.2.1)

error_reporting(E_ALL+E_STRICT);
$arry = array(1=>1);
$nullvar = NULL;
$intvar = 1;

echo $arry[1]; # Echos 1
echo $arry[2]; # Notice: undefined offset.
echo $nullvar[1]; # Echos nothing and no Notice.
echo $intvar[0]; # Ditto
echo $intvar[1]; # Ditto

The manual says that if you convert integer to array, you get an array with
one element at [0]. But that isn't what is happening here, because
$intvar[0] is NULL, not 1, and $intvar[1] does not result in an undefined
offset notice. Instead, I get an array with NULL for any index.

I actually find this behavior useful, but I don't like depending on what
seems to be a quirk.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация