I guess I'm confused do you want to print the global variables to the screen that are named $var2$i? Or do you want to import the global variables into your function?
to print out the globals using a loop you can use a couple of methods:
if (isset($var1)) {
for ($i = 1; $i <= $var1; $i++) {
$varname = "var2$i";
print $GLOBALS[$varname];
}
}
This method uses the GLOBALS array and builds the name of the variable var21, var22, var23, etc. before printing out each one.
if (isset($var1)) {
for ($i = 1; $i <= $var1; $i++) {
$varname "var2$i";
print $$varname;
}
}
This method uses variable variables to print out each item.
If you just want to have the variables accessable inside of a function then you can use the second example above but change the print line to "global $$varname;" or just add the line global $GLOBALS; to the beginning of your function and access all of your variables using that array.