MyBB Community Forums

Full Version: How to echo $x++ while being appended to $_POST['name']?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How to echo $x++ while being appended to $_POST['name']?

$x = 1;
while ($x <= 4) {
	echo $myvariable = $_POST['name'.$x++];
}

I need it to loop and echo 123 but right now it just echos 3.

How can I make it loop, to echo out 123 but still being appended like so "$_POST['name'.$x++]"?

The output of the loop needs to be:

$_POST['name1']
$_POST['name2']
$_POST['name3']
The problem with your code is in the while condition you are using $x as an integer. In the actual code you are trying to make it act as a string.

I'm not sure what your form looks like, but if you use name="something[]" then $_POST['something'] actually returns an array. The array will be indexed as 0, 1, 2, 3, etc. You can loop through each element of the array by using a foreach loop.
Like dragonexpert said, you're trying to concatenate an integer ($x) with a string ($_POST['name']). This should work:
$x = 1;
while($x < 4) {
echo $myvar = $_POST['name' . $x];
$x++;
}

Note I also changed the while loop to
$x < 4
.
I don't know, it works for me? Maybe you are confusing $_POST with $_GET?
It may depend on PHP version. When you using single quotes that can be used to define an array key. In reality a better way to write this would be
$x = 1;
while ($x <= 4) {
++$x;
$key = "name" . $x;
    echo $myvariable = $_POST[$key];
} 
Also, a for loop would be more appropriate than a while one IMO.
for ($i = 1; $i <= 4; $i++) {
    echo $myVariable = $_POST['name' . $key];
}