Question

Quick auestion here, is this a correct syntax to insert $i variable in POST ?

for($i=13; $i<25; $i++)
            if($_POST[''.$i.'']=='on')  array_push($requestedParameters, $_POST[''.$i.'']);

I want to do this instead of writing all of them apart. And how is it called so I could search in internet detailed explanation on concatenation?

Was it helpful?

Solution

Syntax wise, it is correct, but the single quotes are unnecessary as you aren't adding anything on to either end of $i - this would be better:

if($_POST[$i] == 'on')
    array_push($requestedParameters, $_POST[$i]);

You should probably check that the key exists as well, since you are using what looks like an arbitrary loop rather than a foreach type array loop construct:

if(isset($_POST[$i]) && $_POST[$i] == 'on')

OTHER TIPS

All you need to do is $_POST[$i] since it is an integer and not a String.

If it was a String, you would only have to do this: $_POST["{$i}"]

it would just be like this.

$_POST[$i]

no need for the quotes in there. you put those quotes when you want to add some kind of text between them.

for($i=13; $i<25; $i++)
            if($_POST[$i]==='on')  array_push($requestedParameters, $_POST[$i]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top