Howdy...

I know that there were several threads regarding dynamic variable names, but I can't get it working after going over them... 🙁

I am having trouble doing the dynamic variable name in PHP which is my first time practice... 🙂

I did follow multiple combinations that I have found from this forum and from the User notes, but I just can't make it working...

Most of the times, I get this error messages or similar ones...

Notice: Use of undefined constant thisLine - assumed 'thisLine' in d:\inetpub\wwwroot\php\Maybeck4.php on line 40

Now, what I want to do is to have variables like
line0
line1
line2
...
line998
line999

These variables are populated from each line of the pixel data of the image file from the first for loop... and I'd like to create dynamic array items at the second for loop from all of those variables like...
line0_arr
line1_arr
line2_arr
...
But I got stuck at the first place... and I have no idea how to solve this... 🙁

I'd appreciate all the help... 😉

for ($row = 0 ; $row < $imageheight ; $row++)
{
	// I want to have variables like 
	// line0, line1, line2, ... , line998, line999
	$thisLine = "line" . $row;
	$theLine = ${thisLine};
	for ($col = 0 ; $col < $imagewidth ; $col++)
	{
		$rgb = ImageColorAt($image, $col, $row);
		$r = ($rgb >> 16) & 0xFF;
		$g = ($rgb >> 8) & 0xFF;
		$b = $rgb & 0xFF;
		$indexColor = $r + $g + $b;

	if ($indexColor < 10)
		$output .= "1";
	else
		$output .= ".";
	$theLine .= $output;
}
}

for ($row = 0 ; $row < $imageheight ; $row++)
{
	echo(${"line" . $row});
}

    simply use
    ${line . $i} = ...

    where $i stores the number

    but do you really need variable variables? I don't know any case
    where they work and an array doesn't, but there are very powerful
    array functions!!

      change

      $theLine = ${thisLine};

      to

      $theLine = ${$thisLine};

      reg
      kevin

        Thank you for the replies guys...

        Obviously I am not getting it right... I am keep getting this error message...

        Notice: Undefined variable: line0 in d:\inetpub\wwwroot\php\Maybeck4.php on line 40

        Notice: Undefined variable: line1 in d:\inetpub\wwwroot\php\Maybeck4.php on line 40

        Notice: Undefined variable: line2 in d:\inetpub\wwwroot\php\Maybeck4.php on line 40

        Notice: Undefined variable: line3 in d:\inetpub\wwwroot\php\Maybeck4.php on line 40

        ...
        ...

        Besides that, I am not understanding the reasons behind this...

        Let me do this and somebody help me understand this...

        Let's say tha I want to create 10 variables in for loop...
        line0
        line1
        line2
        ...
        line8
        line9

        and I want to assign the value from 0 to 9 to each variables...
        Can somebody tell me if I am right???

        <?
        	for ($i = 0 ; $i < 10 ; $i++)
        	{
        		$lineVar = "line" . $i;
        		$theLine = ${$lineVar};
        		$theLine = $i;
        		echo("i = " . $i . "\ttheLine = " . $theLine);
        	}
        ?>

        and the result is

        Notice: Undefined variable: line0 in d:\inetpub\wwwroot\php\dynamicVars.php on line 6
        i = 0 theLine = 0
        Notice: Undefined variable: line1 in d:\inetpub\wwwroot\php\dynamicVars.php on line 6
        i = 1 theLine = 1
        Notice: Undefined variable: line2 in d:\inetpub\wwwroot\php\dynamicVars.php on line 6
        i = 2 theLine = 2
        Notice: Undefined variable: line3 in d:\inetpub\wwwroot\php\dynamicVars.php on line 6
        i = 3 theLine = 3
        Notice: Undefined variable: line4 in d:\inetpub\wwwroot\php\dynamicVars.php on line 6
        i = 4 theLine = 4
        Notice: Undefined variable: line5 in d:\inetpub\wwwroot\php\dynamicVars.php on line 6
        i = 5 theLine = 5
        Notice: Undefined variable: line6 in d:\inetpub\wwwroot\php\dynamicVars.php on line 6
        i = 6 theLine = 6
        Notice: Undefined variable: line7 in d:\inetpub\wwwroot\php\dynamicVars.php on line 6
        i = 7 theLine = 7
        Notice: Undefined variable: line8 in d:\inetpub\wwwroot\php\dynamicVars.php on line 6
        i = 8 theLine = 8
        Notice: Undefined variable: line9 in d:\inetpub\wwwroot\php\dynamicVars.php on line 6
        i = 9 theLine = 9

        Can somebody tell me what I am geting those error messages???

        Now, I have added another for loop right below the first for loop...

        	for ($j = 0 ; $j < 10 ; $j++)
        {
        	$lineVar = "line" . $j;
        	$theLine = ${$lineVar};
        	echo("j = " . $j . "\ttheLine = " . $theLine);
        }

        and I am not able to get the value from it...

        Notice: Undefined variable: line0 in d:\inetpub\wwwroot\php\dynamicVars.php on line 12
        j = 0 theLine =
        Notice: Undefined variable: line1 in d:\inetpub\wwwroot\php\dynamicVars.php on line 12
        j = 1 theLine =
        Notice: Undefined variable: line2 in d:\inetpub\wwwroot\php\dynamicVars.php on line 12
        j = 2 theLine =
        Notice: Undefined variable: line3 in d:\inetpub\wwwroot\php\dynamicVars.php on line 12
        j = 3 theLine =
        Notice: Undefined variable: line4 in d:\inetpub\wwwroot\php\dynamicVars.php on line 12
        j = 4 theLine =
        Notice: Undefined variable: line5 in d:\inetpub\wwwroot\php\dynamicVars.php on line 12
        j = 5 theLine =
        Notice: Undefined variable: line6 in d:\inetpub\wwwroot\php\dynamicVars.php on line 12
        j = 6 theLine =
        Notice: Undefined variable: line7 in d:\inetpub\wwwroot\php\dynamicVars.php on line 12
        j = 7 theLine =
        Notice: Undefined variable: line8 in d:\inetpub\wwwroot\php\dynamicVars.php on line 12
        j = 8 theLine =
        Notice: Undefined variable: line9 in d:\inetpub\wwwroot\php\dynamicVars.php on line 12

        It looks like the first for loop did not create those 10 variables, line0, line1, ... , line9...

        Can someone tell me what I am doing wrong??? Thank you for your time... 😉

          please use arrays for those cases:

          <?
              error_reporting (E_ALL ^ E_NOTICE);
          
          $theLine = array();
          for ($i = 0 ; $i < 10 ; $i++)
          {
              $theLine[$i] = $i;
              echo("i = " . $i . "\ttheLine = " . $theLine[$i]);
          }
          ?>

          note: these messages are only notices, no errors
          to suppress them use
          error_reporting (E_ALL ^ E_NOTICE);
          as first line

            does this help

            <?
            for ($i = 0 ; $i < 10 ; $i++){
            $lineVar = "line".$i;
            echo $lineVar." = ";
            $lineVar = $i;
            echo $lineVar."<BR>";
            }
            ?>

            reg
            kevin

              Yup... I got it working now... 😉

              ir4z0r's first help, ${line . $i}, didn't work in the beginning, but i does work now... I don't know what I did, but it does work... 😉

              But the example that I have showed you, which are basically from the PHP User Guide, is not really getting into my brain yet... I don't understand why I have to reference the variable that way to assign/retrieve the value... Oh, well... As long as it works... 😉

              Thank you, ir4z0r and kevinsequeira for the great help... 😉

              Oh, ir4z0r... I need it to be the string literal because I am sending this result back to Flash, and Flash cannot read PHP array information without the Flash Remoting Component...

                Write a Reply...