Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Direct Answer to Question</h2> <p>This is how you can do it. Not the best idea however.</p> <pre><code>$var = "$Site$i"; $$var = $_GET['site'.$i]; </code></pre> <p>This makes use of <a href="http://php.net/manual/en/language.variables.variable.php" rel="nofollow noreferrer">variable variables</a>.</p> <h2>Alternative Maintaining Current URL Structure</h2> <p>Alternatively perhaps something like this might work for you:</p> <pre><code>$vars = array(); foreach($_GET as $key =&gt; $value) { if(0 === strpos($key, 'site')) { // Only grab value if the key is prefaced by the string 'site' // You must sanitise the value some way here eg: // $value = filter_var($value, FILTER_SANITIZE_STRING); $vars[] = $value; } } </code></pre> <p>See <a href="http://php.net/manual/en/function.filter-var.php" rel="nofollow noreferrer">filter_var() man page</a> for more information on PHP filters and sanitisation/validation.</p> <h2>Revised URL Structure</h2> <p>I think this probably best solved however by making use of HTML arrays at the point your URL is generated. For more information on HTML arrays please see the <a href="http://www.php.net/manual/en/faq.html.php#faq.html.arrays" rel="nofollow noreferrer">PHP man page</a>.</p> <p>This allows you to access your information like the following:</p> <pre><code>$site1 = $_GET['site'][0]; $site2 = $_GET['site'][4]; </code></pre> <p>This is the most logical method of dealing with this situation.</p> <p><strong>Update</strong> also see <a href="https://stackoverflow.com/questions/6199744/dynamically-create-variables-in-php/6199877#6199877">@Mat's answer</a> for more information on this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload