PDA

View Full Version : PHP eval problems



SlowRoasted
October 18th, 2006, 02:32 PM
I have variables $q1 - $q50. I want to loop through and see if they are set before I go on to the next bit of code. I am trying to use a for loop and eval. I think I am using eval incorrectly, probably because I am so used to actionscript. Can anyone take a loot at this for me and tell me what I'm doing wrong?



for($i=1;$i<51;$i++){
if(isset(eval("$q".$i))){
$valid = TRUE;
}else{
$valid = FALSE;
echo "Please make sure all questions are answered.";
break;
}
}

bwh2
October 18th, 2006, 02:58 PM
i don't know of a way in PHP to handle dynamic variable names like how you're suggesting. my advice is to use an array. then you could use a simple foreach.

SlowRoasted
October 18th, 2006, 03:24 PM
thx, I'll try validating some other way.

bwh2
October 18th, 2006, 03:25 PM
like this:


$q = array();
$q[0] = 'zero';
$q[1] = 'one';
$q[2] = 'two';
$q[3] = 'three';
$q[4] = '';
$q[5] = 'five';

$valid = TRUE;

foreach( $q as $key=>$val ) {
if( !isset($val) || $val=='' ) {
echo 'You must fill in all questions.';
$valid = FALSE;
last;
}
echo $val.'<br />';
}