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
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
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'.
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};
}
You should use an array rather than individual variables.
For reference:
This should work..
foreach($_GET as $id => $value){
echo $value;
}
even though this prints out every $_GET.
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;
}
}
?>
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];
}
?>