Basic Question.....I think?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gregTelevirtual
Forum Newbie
Posts: 3
Joined: Thu Jul 12, 2007 6:35 am

Basic Question.....I think?

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.)
gregTelevirtual
Forum Newbie
Posts: 3
Joined: Thu Jul 12, 2007 6:35 am

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
gregTelevirtual
Forum Newbie
Posts: 3
Joined: Thu Jul 12, 2007 6:35 am

Post by gregTelevirtual »

thank you!

I got it.

all the arrays should be assigned like this

$originalArray[$i] NOT $originalArray

it works now.

cheers

Greg
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Good job.
Post Reply