0

I have an associative array $_POST, which has 3 key value pairs (There are other key value pairs which I am not interested in).

$_POST[Var1]
$_POST[Var2]
$_POST[Var3]

How do I use a for loop to loop through and echo the values in each?

for ($i = 1; $i <= 3; $i++){
    echo $_POST['Var' . '$i']; 
}

This does not seem to work.

3 Answers 3

5

Get rid of the single quotes around $i as that makes it a literal string and your variable is not interpolated:

for ($i = 1; $i <= 3; $i++){
    echo $_POST['Var' . $i]; 
}

This is basic PHP. I strongly recommend reading the manual to learn more about the fundamentals of PHP.

Sign up to request clarification or add additional context in comments.

Comments

0

Check this. While using php varibles dont put upper commas for the variables.

  <?php
    $_POST["file1"];
    $_POST["file2"];
    $_POST["file3"];
    for ($i = 1; $i <= 3; $i++){
        echo $_POST['file'.$i]; 
    }

Comments

0

Or this way:

$_POST["Var{$i}"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.