...

View Full Version : Resolved Using "null" for first value in array?



RonnyNishimoto
07-11-2012, 12:07 AM
I'm not very comfortable counting from 0, is there any problem with just using:



$var = array(null, "Mike"=>22);


In terms of future languages and the mindset I should be in, would this be a bad idea? Does anyone else use null in order to start at 1?

Thank you!

firepages
07-11-2012, 01:42 AM
In an associative array like yours above there seems little point since you would reference the values by the key , e.g. $var['mike'] not $var[1];
+ if you iterate through your arrays with foreach( etc I don't see how the first value having an index of 0 is of too much concern.

Personally I would say just get used to counting from 0, if you have an empty value at the start of an array you may later need to add an extra check for such which is IMO wasted code..e.g.


<?
foreach($arr as $k=>$v){
echo "<div id=\"$k\">$v</div>";
}
?>
now needs to be...
<?
foreach($arr as $k=>$v){
if(!empty($v)){
echo "<div id=\"$k\">$v</div>";
}
}
?>


else you get an empty div ... you may need to do such a check for other reasons but with null values you have to do this everywhere ?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum