Forum Moderators: coopster
I'm carrying large numbers of variables through different pages and I wondered if there is a way to simplify this?
In the example below, I am collecting 100 variables from a form on the previous page but have to write them all out at the moment. Can PHP help me reduce this down?
$q1=$_POST['q1'];
$q2=$_POST['q2'];
$q3=$_POST['q3'];
$q4=$_POST['q4'];
$q5=$_POST['q5'];
...
$q100=$_POST['q100'];
Many thanks
Ed
Assuming there are no issues with namespace collisions (i.e. the indexes of the post variables don't conflict with other existing variables), the simplest way is just:
extract [php.net]($_POST);
If you want to make sure that you don't overwrite existing variables (say the $q series is okay, but there are other post vars that have index names that are the same as other variable names), then you can add some flags like so:
extract($_POST, EXTR_PREFIX_SAME, "post_");
If $_POST['q1'] = "I'm a post"; and $q1 = "I'm not a post"; then in the first case, after running extract(), $q1 will be "I'm a post", in the second example, $q1 will have it's original value and $post_q1 will be "I'm a post".