2

look at this simple script please

$c1 = $_GET[c1];
$c2 = $_GET[c2];
$c3 = $_GET[c3];
$c4 = $_GET[c4];
$c5 = $_GET[c5];
for($i = 1;$i <=5;$i++)
{
    echo $c{$i};//or something else here :/
}

how can i print tha values of variables?

Thanks

2
  • 2
    If you are defining the variables, use an array instead. Could someone explain why so many are suggesting alternatives to a plain-old array? Commented Jul 23, 2010 at 12:34
  • possible duplicate of Can I use a generated variable name in PHP? Commented Oct 1, 2010 at 4:04

6 Answers 6

4

You can see on php.net some good examples in the variable page. Read that and take a look at the examples.

Also, below is your piece of code fixed so it can work:

<?php

$c1 = $_GET[c1];
$c2 = $_GET[c2];
$c3 = $_GET[c3];
$c4 = $_GET[c4];
$c5 = $_GET[c5];
for($i = 1;$i <=5;$i++)
{
    echo ${"c".$i};
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is not a good thing to advise the guy to do. This is a case where arrays are the most appropriate answer; why tell him to use variable variables?
He wanted variable variables (probably because in $c1, $c2 etc checks the $_GET contents. If he would have asked for other solutions, I would suggest others, but it seems to me this questions is about variable variables.
This answer is more correct, but recommending arrays is better. In cases like these, I feel that the best answers are a merging of yours and @Kieren Johnstone: they offer the solution to the question at hand, but also make a point of saying that it's really not good practice. Regardless, +1 to you both xP
3

You should use an array rather than individual variables.

For reference:

http://php.net/manual/en/language.types.array.php

Comments

3

If these values are closely related, consider changing their name attribute in your HTML/form.

HTML:

<form>
    <input type="text" name="c[]" />
    <input type="text" name="c[]" />
    ...
</form>

PHP:

<?php

    if(!empty($_GET['c'])) {
        foreach($_GET['c'] as $c) {
            echo $c;
        }
    }

?>

Comments

1

Here's a better way of doing it, using Arrays, rather than individual variables, which works easier and more efficiently.

<?php
$array['c1'] = $_GET['c1'];
$array['c2'] = $_GET['c2'];
$array['c3'] = $_GET['c3'];
$array['c4'] = $_GET['c4'];
$array['c5'] = $_GET['c5'];
for ($i=1; $i>=5; $i++) {
    echo $array['c' . $i];
}
?>

Comments

0

Perhaps PHP variable variables is what you are looking for.

$i = "c1";
print $$i;

I'll leave it to you to figure out how to construct correct values for 'i'.

Comments

-2

This should work..

 foreach($_GET as $id => $value){
      echo $value;
 }

even though this prints out every $_GET.

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.