PDA

View Full Version : Bit of a PHP brainteaser


jodieorourke
04-10-2006, 03:07 PM
I'm further down the line with my code than I was the last time I posted, but I've encountered another problem. My FOR loop doesn't appear to execute unless I add a numerical value as the condition (as opposed to a variable).

Example:

extract($_POST, EXTR_PREFIX_SAME, "myVar");

// $expectedItems is created, with a value of 8

for($i=0; $i<$expectedItems; $i++){
$varName1 = "itemIdArray" . $i;
$varName2 = "itemCatArray" . $i;
$varName3 = "itemTitleArray" . $i;
$varName4 = "itemDescArray" . $i;
$varName5 = "itemLocationArray" . $i;
$varName6 = "itemImgArray" . $i;

// Do some stuff with these variables
}

If I swap $expecteItems for the value '10' in the loop conidtion, the loop runs 10 times, as would be expected. So why doesn't it work with the variable, which clearly has a value of '8'??? I'm thinking this must be a problem with the typing of the variable, but I though variables in PHP weren't strictly typed?

I'm nearly there, and any help would be gratefully received!

CamX
04-11-2006, 11:37 PM
How are you setting $expectedItems?

Flash Gordon
04-11-2006, 11:53 PM
scope issue. Make sure your have _global when you make your variable.

jsebrech
04-12-2006, 09:29 AM
What happens if you echo $expectedItems before the loop?