Page 1 of 1

Basic Question.....I think?

Posted: Thu Jul 12, 2007 7:06 am
by gregTelevirtual
Hi,

I'm relatively new to php been doing it a couple of weeks and i have a what i think is a basic question. Here is my code.

Code: Select all

<?php
	$max = $_POST["max"];
        $newArray = array();
        $print = FALSE;
	for($i=0;$i<$max;$i++)
	{
        $originalArray[i] = $_POST["text$i"];
            
		if($i == $max - 3 && $print == FALSE)
		{
			$newArray[0] = $originalArray[i];
			//echo "     $newArray[0] <br>";
		}
		if($i == $max - 2 && $print == FALSE)
		{
			$newArray[1] = $originalArray[i];
			//echo "     $newArray[1] <br>";
		}
		if($i == $max - 1 && $print == FALSE)
		{
			$newArray[2] = $originalArray[i];
			//echo "     $newArray[2] <br>";
			$print = TRUE;
			$i = 0;
		}
			
		if($i < $max - 3 && $print == FALSE)
		{
			for($k=$i+3;$k<$i+4;$k++)
			{
	      			$newArray[k]=$originalArray[i];
				//echo "    $newArray[k] ";
       			}
       		}
       			
		/*why doesn't this loop know what's inside newArray is when it is declared and assigned above*/
		if($print == TRUE)
		{
			echo "$i = $newArray[i]";			
			if((($i+1) % 3) == 0)
			{
				echo "<br>";
			}
		}	

	}
?>
and the output is

Code: Select all

0 = 1 = 2 =
3 = 4 = 5 =
6 = 7 = 8 =
9 = 10 = 11 =
12 = 13 = 14 =
15 = 16 = 17 =
18 = 19 = 20 =
21 = 22 = 23 =
24 = 25 = 26 =
why doesnt the last loop know what's inside $newArray[] is when it is declared and assigned above.

thanks

Greg

Posted: Thu Jul 12, 2007 7:26 am
by feyd
Your indices "i" and "k" aren't variables.

Make sure your error_reporting setting is E_ALL instead of E_ALL ^ E_NOTICE (or similar variants.)

Posted: Thu Jul 12, 2007 8:52 am
by gregTelevirtual
Thanks for the reply

i and k are variables, the code runs which suggests that syntactically it's correct?

The error settings are already E_ALL.

Posted: Thu Jul 12, 2007 10:42 am
by RobertGonzalez
The way the code is now 'i' and 'k' are constants, not variables. Look closely at them and see if you can spot why PHP would assume they are constants.

Posted: Thu Jul 12, 2007 10:53 am
by gregTelevirtual
thank you!

I got it.

all the arrays should be assigned like this

$originalArray[$i] NOT $originalArray

it works now.

cheers

Greg

Posted: Thu Jul 12, 2007 10:53 am
by RobertGonzalez
Good job.