vendredi 21 avril 2017

Automatization of a conditional function through the use of an array to test postdata variables

I have some problems to make the following automatization:

I get from a form a lot of variables such as $a and $b.
I would like to automatize a conditional test depending on the variables I obtain.

$test = array('a','b');
for($i=0;$i<sizeof($test);$i++)

For the 1st variable a, the working code is:

if ( $postdata->a !=${$test[$i]}){echo "different";} else {echo "same";}  }

For the 2nd variable b, the working code is:

if ( $postdata->b !=${$test[$i]}){echo "different";} else {echo "same";}  }

I would like to automatize it thanks to the array like this:

if ( $postdata->$test[$i] !=${$test[$i]}){echo "different";} else {echo "same";}  }

but $postdata->$test[$i] doesn't work, even echo $test[$i] gives 'a' and 'b'. I tried several "writing" but I couldn't solve it. Thank you for your help.

Aucun commentaire:

Enregistrer un commentaire