tags:

views:

313

answers:

4

I am currently using the following code for Get variables, and would like to know the best way to convert to an array. Is the main advantage of doing this just aesthetic, or is there a performance/efficiency aspect as well?

if (isset($_GET["cmd"]))
  $cmd = $_GET["cmd"];
else
die("Invalid URL");
  if (isset($_GET["username"]))
 { $username = $_GET["username"];}
  if (isset($_GET["firstname"]))
 { $firstname = $_GET["firstname"];}
  if (isset($_GET["lastname"]))
 { $lastname = $_GET["lastname"];}
  if (isset($_GET["street1"]))
 { $street1 = $_GET["street1"];}
  if (isset($_GET["city1"]))
 { $city1 = $_GET["city1"];}
  if (isset($_GET["postcode1"]))
 { $postcode1 = $_GET["postcode1"];}
  if (isset($_GET["street2"]))
 { $street2 = $_GET["street2"];}
  if (isset($_GET["city2"]))
 { $city2 = $_GET["city2"];}
  if (isset($_GET["postcode2"]))
 { $postcode2 = $_GET["postcode2"];}  
 if (isset($_GET["phone"]))
 { $phone = $_GET["phone"];}  
 if (isset($_GET["mobilephone"]))
 { $mobilephone = $_GET["mobilephone"];}  
 if (isset($_GET["fax"]))
 { $fax = $_GET["fax"];}  
 if (isset($_GET["email"]))
 { $email = $_GET["email"];}  
 if (isset($_GET["website"]))
 { $website = $_GET["website"];}  
 if (isset($_GET["bank"]))
 { $bank = $_GET["bank"];}  
 if (isset($_GET["banknumber"]))
 { $banknumber = $_GET["banknumber"];}  
 if (isset($_GET["accountnumber"]))
 { $accountnumber = $_GET["accountnumber"];}  
  if (isset($_GET["subcat"]))
 { $subcat = $_GET["subcat"];}
+1  A: 

You can use extract, but don't, unless you're going to be careful.

From the doc:

Import variables from an array into the current symbol table.

Checks each key to see whether it has a valid variable name. It also checks for collisions with existing variables in the symbol table.

But be warned:

Do not use extract() on untrusted data, like user-input ($_GET, ...). If you do, for example, if you want to run old code that relies on register_globals temporarily, make sure you use one of the non-overwriting extract_type values such as EXTR_SKIP and be aware that you should extract in the same order that's defined in variables_order within the php.ini.

karim79
+3  A: 

Basically, if you are doing repetition like the above then there is usually a better way of doing things. If you have a collection data, as it looks like you do, it is always generally best to keep it in an array.

If you are getting it from a form you can actually put it in an array in the form itself, using:

<input type="text" name="details[username]" />
<input type="text" name="details[first_name]" />
etc.

The you will get the results in the form of $_GET['details'] which can be iterated through using

foreach ($_GET['details'] as $field_name=>$value)
{
    echo "<p>{$field_name} = {$value}</p>";
}

Which should display the values of all data entered in the form. The code you put in the loop to deal with it is up to you. You can, for example, build a SQL statement inside the foreach which will be written on submit - just watch out for SQL injection and the user adding fields.

Meep3D
+1  A: 

Group the default values in an array:

$default = array(
    'email' => '',
    'name' => NULL,
    //...
);

Then merge your table and extract the result in variables:

extract( array_merge( $default, $_GET ), EXTR_PREFIX_SAME, 'param' );
// here already existing variable names will be prefixed with 'param_'

And use with empty() test when necesssary:

if( empty( $name ) )
    die( "You must specify the name." );
streetpc
A: 

Not sure about the question. $_GET is an array.

txwikinger