NYCPHP Meetup

NYPHP.org

[nycphp-talk] Array handling - Why does this happen?

DeWitt, Michael mjdewitt at alexcommgrp.com
Thu Feb 12 12:29:58 EST 2004


I have run into a situation with arrays that I can't figure out why value
assigments are truncated down to one byte.

Consider the array

$question['1']=array('type'=>'radio','showresults'=>1,'required'=>0,'questio
n'=>'Does your community post signs on neglected properties with the
owner’s name and address clearly visible to all passersby?',
'answer'=>array('Yes'=>array('Yes'=>'Yes','count'=>0),'No'=>array('No'=>'No'
,'count'=>0)));

$q_id=1;
$val='Yes';
$val1='No';

$question[$q_id]['answer'][$val]['count']=8888;

echo $question[$q_id]['answer'][$val]['count']."<br>";

$question[$q_id]['answer'][$val1]['count']=9999;

echo $question[$q_id]['answer'][$val1]['count']."<br>";


will echo correctly the numbers assigned to 'count' of 8888 and 9999

However for this array,

$question['1']=array('type'=>'radio','showresults'=>1,'required'=>0,'questio
n'=>'Does your community post signs on neglected properties with the
owner&#146;s name and address clearly visible to all passersby?',
'answer'=>array('Yes'=>'Yes','No'=>'No'));

$q_id=1;
$val='Yes';
$val1='No';

$question[$q_id]['answer'][$val]['count']=8888;

echo $question[$q_id]['answer'][$val]['count']."<br>";

$question[$q_id]['answer'][$val1]['count']=9999;

echo $question[$q_id]['answer'][$val1]['count']."<br>";

will echo back 'count' as being 8 and 9

I thought in PHP you could extend arrays with new elements dynamically on
assignment and if you can't, why wouldn't this error out rather than merely
truncating to one byte?

Or, perhaps, I am thinking about this all wrong???


Mike



More information about the talk mailing list