Bifterss
Bifterss

Reputation: 293

Dynamically create variables in PHP

I want to create 1 variable name, but part of the name is the value stored in $i. Same for the GET result:

$Site.$i = $_GET['site'.$i]; // Should look something like $Site1 = $GET['site1'];

Please help me understand how to do this.

Upvotes: 1

Views: 4099

Answers (5)

Treffynnon
Treffynnon

Reputation: 21553

Direct Answer to Question

This is how you can do it. Not the best idea however.

$var = "$Site$i";
$$var = $_GET['site'.$i];

This makes use of variable variables.

Alternative Maintaining Current URL Structure

Alternatively perhaps something like this might work for you:

$vars = array();
foreach($_GET as $key => $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;
    }
}

See filter_var() man page for more information on PHP filters and sanitisation/validation.

Revised URL Structure

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 PHP man page.

This allows you to access your information like the following:

$site1 = $_GET['site'][0];
$site2 = $_GET['site'][4];

This is the most logical method of dealing with this situation.

Update also see @Mat's answer for more information on this.

Upvotes: 2

Mat
Mat

Reputation: 6725

If you want a set of related variables, use an array:

$site[ $i ] = $_GET['site'.$i];

Even better, your GET parameters can also be an array

HTML

<input name="site[foo]" value="bar" />

PHP

$site = $_GET[ "site" ];

print_r( $site );

output

$site = array(
  "foo" => "bar"
)

If you want the indexes for the array to decided automatically then you can do

<input name="site[]" value="foo" />
<input name="site[]" value="bar" />
<input name="site[]" value="baz" />

and get $_GET[ "site" ] out as

$site = array(
  0 => "foo",
  1 => "bar",
  2 => "baz"
);

Upvotes: 4

Emil Vikstr&#246;m
Emil Vikstr&#246;m

Reputation: 91942

This is a bad idea for several reasons:

  • You have to loop through $_GET to find all variables (there's no language construct to pattern-match them)
  • Dynamic variables names are confusing, and may open security holes.

You will find that using an array will solve the second point, and also make it a lot easier to work with the code.

The first point can be solved by only using variable names you know. Send a variable containing a count how how many "sites" there are, for example:

site1=example&site2=example2&sitecount=2

This way you know that you only need to read site1 and site2, and you donät need to examine any other GET variables.

Upvotes: 1

Adam Arold
Adam Arold

Reputation: 30528

You can use variable variables like this:

$varname = $Site.$i;
$$varname = $_GET['site'.$i];

Doing this is discouraged however, because this is a huge security risk. You may write classes with fields representing your values from $_GET and validating them within the class.

Upvotes: 0

you van use $ as $_GLOBAL like this.

${'Site' . $i} = $_GET['site' . $i];

or you can use extract

please read the warnings about exract.

Upvotes: 0

Related Questions