55502f40dc8b7c769880b10874abc9d0

I'm using the following code to make sure that a handful of $_GET values are always set. If they aren't set in the original $_GET, I create them by giving them the value of "null".

Can this be done cleaner? PHP's array_map sounds good, but results in less clean code, in my opinion.

EDIT: I like Scott Reynen's solution best. It does exactly what I need, in a single onliner :)

$getvalues = array('id', 'page', 'var1, 'var2', 'var3');

foreach($getvalues as $getvalue)
{
  if(!isset($_GET[$getvalue]))
  {
    $_GET[$getvalue] = null;
  }
}

Refactorings

No refactoring yet !

D41d8cd98f00b204e9800998ecf8427e

Vilx-

November 16, 2009, November 16, 2009 10:15, permalink

1 rating. Login to rate!

I usually create a GET and POST wrapper functions. Something along these lines:

function GET($name, $default=null)
{

}
D41d8cd98f00b204e9800998ecf8427e

Vilx-

November 16, 2009, November 16, 2009 10:15, permalink

1 rating. Login to rate!

I usually create a GET and POST wrapper functions. Something along these lines:

function GET($name, $default=null)
{

}
D41d8cd98f00b204e9800998ecf8427e

Vilx-

November 16, 2009, November 16, 2009 10:18, permalink

1 rating. Login to rate!

Something went really wrong. Trying again. This approach also has the benefit that you can put the stripslashes() there if needed (or even create a recursive version of stripslashes if you are using arrays in GET/POST values).

function GET($name, $default=null)
{
	if ( isset($_GET[$name]) )
		return $_GET[$name];
	return $default;
}
55502f40dc8b7c769880b10874abc9d0

10goto10.myopenid.com

November 16, 2009, November 16, 2009 11:40, permalink

No rating. Login to rate!

Hi Vilx, but this setup still needs to itterate through an array of "getvalues" that need to be checked. Or do you mean to call your GET-function using array_map?

330555fba743b009765f9d38d7137e6f

Scott Reynen

November 16, 2009, November 16, 2009 13:17, permalink

2 ratings. Login to rate!

Untested, but array_merge should work for this as long as your keys are non-numeric.

$getvalues = array('id' => null, 'page' => null, 'var1' => null, 'var2' => null, 'var3' => null);
$_GET = array_merge($getvalues, $_GET);
F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

November 16, 2009, November 16, 2009 16:54, permalink

No rating. Login to rate!

If only PHP did not suck so much you could do this:

function GET($key, $default = null) {
  return $_GET[$key] || $default;
}
D85d44a0eca045f40e5a31449277c26c

Ben Marini

November 16, 2009, November 16, 2009 17:15, permalink

1 rating. Login to rate!

I might be tempted to do something like this.

<?php
  // $id = Request::get("id");
  class Request {
    public static function get($key, $default=null) {
      return array_key_exists($key, $_GET) ? $_GET[$key] : $default;
    }
    
    public static function post() {
      // ...
    }
  }
D41d8cd98f00b204e9800998ecf8427e

bob

November 16, 2009, November 16, 2009 19:32, permalink

No rating. Login to rate!
$_GET = array_merge(array_fill_keys($getvalues, null), $_GET);
34db50b7ce2e115afadf5a765b950739

Thomas Salvador

November 16, 2009, November 16, 2009 19:39, permalink

1 rating. Login to rate!

hi.
i would leave it pretty much like it is, but use the '?'-notation:
traverse through the keys you want to assure and create a $_GET entry for it by assignment. if entry is already there use its original value, null otherwise.

regards,
thomas.

$getvalues = array('id', 'page', 'var1, 'var2', 'var3');

foreach($getvalues as $getvalue) {
  $_GET[$getvalue] = (isset($_GET[$getvalue]) ? $_GET[$getvalue] : null );
}
34db50b7ce2e115afadf5a765b950739

Thomas Salvador

November 16, 2009, November 16, 2009 19:41, permalink

1 rating. Login to rate!

hi again.

this approach could also be used for the function/getter-style, mentioned above.

regards,
thomas.

function GET($key, $default = null) {
  return (isset($_GET[$key]) ? $_GET[$key] : $default );
}

Your refactoring





Format Copy from initial code

or Cancel