Jump to content

Correct syntax for loop/variable names


golden14

Recommended Posts

I have many variables (about 50) coming into PHP from Flash, and I wanted to optimize my PHP code using a loop, but I can't seem to get the correct syntax.  Here is the working code I have so far (using just 3 as an example):

 

<?php

$tName1=$_POST['tName1'];
$tName2=$_POST['tName2'];
$tName3=$_POST['tName3'];

echo "tName1=".$tName1;
echo "tName2=".$tName2;
echo "tName3=".$tName3;
?> 

 

And I would like to do that in a loop.  Here's my incorrect attempt at that:

 

<?php

for ($i=1; $i<4; $i++) {

$tName[$i]=$_POST['tName[$i]'];
echo "tName$i"=.$tName$i;
}
?>

 

Any suggestions/help would be much appreciated, thanks!

Link to comment
Share on other sites

Variable variables, introduced in PHP5 I do believe

 

<?php
for ($i=1; $i<4; $i++) {
$tName = "tName" . $i;

$tName = $_GET[$tName];
echo "tName" . $i . "=" . $tName . "<br />";
}
?>

 

I used GET for when I tested (you can change that)

 

I accessed it from http://localhost/PHP/test.php?tName1=testing1&tName2=lalalalal&tName3=test3

 

my output was

tName1=testing1
tName2=lalalalal
tName3=test3

 

 

Hope this was useful!!

Link to comment
Share on other sites

Thank you both for the help.  The first bit of code didn't seem to work, so I tried the second way (changing GET to POST), and that worked well.  You mention that doing it the second way won't allow me to access the variables later on, but since I POST instead of GET.  Was that different in not allowing me to access them again later?

 

Thanks again for the help.

Link to comment
Share on other sites

what's different in the second one is that the variable $tName is used over and over. after the loop, it is set to the last value but you won't have any before the last one. my version is working fine here:

 

<?php
// set these for testing
$_POST['tName1'] = "var1";
$_POST['tName2'] = "var2";
$_POST['tName3'] = "var3";

for ($i=1; $i<4; $i++) {
     ${'tName'.$i}=$_POST['tName'.$i];
      echo "tName$i = ".${'tName'.$i}."<BR>";
}
?>
[code]

output:
tName1 = var1
tName2 = var2
tName3 = var3

[/code]

Link to comment
Share on other sites

in my example, you can't access $tname1,$tname3, $tname3, etc from outside of the loop (because theoretically it does not exist).

 

 

Also BlueSkyIS's script runs fine on my server.  I haven't played around a whole lot in variable variables but his example is great.

Link to comment
Share on other sites

Thanks for that explanation, that makes sense (too much sense now that I think about it).

 

I had to make a few small changes, because due to the nature of how it comes back into the various text fields in flash, I had to use an "&" to split it up instead of the "<BR>".

 

So here's the code now, and this does work correctly.  Thanks for the help and taking the time to explain things, I really appreciate it.

 

<?php
for ($i=1; $i<4; $i++) {
${'tName'.$i}=$_POST['tName'.$i];

if($i>1){
echo "&tName".$i. "=".${'tName'.$i};
}
else echo "tName".$i. "=".${'tName'.$i};
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.