-2

I have a page that takes a lot of $_GET parameters but it's not always guaranteed for all fo them to be used. So i get a lot of warnings right now for undefined variables. I obvously need to check if it's set with isset so i avoid warnings but i have literally 80!!!

Just showing few for example (but they are much more than that, maybe 80)

$pt2r2c1 = htmlspecialchars($_GET['pt2r2c1']);
$pt2r2c2 = htmlspecialchars($_GET['pt2r2c2']);
$pt2r2c3 = htmlspecialchars($_GET['pt2r2c3']);
$pt2r3c1 = htmlspecialchars($_GET['pt2r3c1']);
$pt2r3c2 = htmlspecialchars($_GET['pt2r3c2']);
$pt2r3c3 = htmlspecialchars($_GET['pt2r3c3']);
$pt2r4c1 = htmlspecialchars($_GET['pt2r4c1']);
$pt2r4c2 = htmlspecialchars($_GET['pt2r4c2']);
$pt2r4c3 = htmlspecialchars($_GET['pt2r4c3']);
$pt2r5c1 = htmlspecialchars($_GET['pt2r5c1']);
$pt2r5c2 = htmlspecialchars($_GET['pt2r5c2']);

So i started doing this manually ... but at the 40th i felt stupid and i thought if there is a better way to do it.

if (isset($_GET['pt1r5c3'])) {
$pt1r5c3 = htmlspecialchars($_GET['pt1r5c3']);
}
if (isset($_GET['pt1r6c1'])) {
$pt1r6c1 = htmlspecialchars($_GET['pt1r6c1']);
}
if (isset($_GET['pt1r6c2'])) {
$pt1r6c2 = htmlspecialchars($_GET['pt1r6c2']);
}
if (isset($_GET['pt1r6c3'])) {
$pt1r6c3 = htmlspecialchars($_GET['pt1r6c3']);
}
if (isset($_GET['pt2r1c1'])) {
$pt2r1c1 = htmlspecialchars($_GET['pt2r1c1']);
}
if (isset($_GET['pt2r1c2'])) {
$pt2r1c2 = htmlspecialchars($_GET['pt2r1c2']);
}
if (isset($_GET['pt2r1c3'])) {
$pt2r1c3 = htmlspecialchars($_GET['pt2r1c3']);
}
Djongov
  • 195
  • 2
  • 13
  • 1
    Why so many GET parameters? This is near impossible to maintain past a certain quantity, why not [pass them as an array](https://stackoverflow.com/questions/7206978/how-to-pass-an-array-via-get-in-php) and iterate it? – esqew Nov 25 '20 at 15:33
  • 1
    You could have an array of these names, loop over the array, and create variables from each of the names, if you really wanted. https://www.php.net/manual/en/language.variables.variable.php – Martin Burch Nov 25 '20 at 15:33
  • 1
    Use a loop, it is a simple as that. If the variable names follow some structure, you can even auto-generate them in your loop. – Polygnome Nov 25 '20 at 15:35
  • @Polygnome may i have an example of that please? – Djongov Nov 25 '20 at 15:37
  • @MartinBurch i sitll need to check if they are set, correct? – Djongov Nov 25 '20 at 15:49
  • @Djongov `$variables = ['foo', 'bar']; foreach($variables as $var) { $$var = "val"; } echo $foo; echo $bar;` – Polygnome Nov 25 '20 at 15:58

1 Answers1

1

Ok thanks to some hints in the comments i realised i can just try with a simple array and foreach. So after some trial and error i managed to do it that way. Hope it help others.

   $tree_variables_array = ['pt1r1c1', 'pt1r1c2', 'pt1r1c3', 'pt1r2c1'];
        
        
    foreach ($tree_variables_array as $var) {
      if(isset($_GET[$var])) {
        $$var = htmlspecialchars($_GET[$var]);
      }
    }
Djongov
  • 195
  • 2
  • 13