$getvalues = array('id', 'page', 'var1, 'var2', 'var3');
foreach($getvalues as $getvalue)
{
if(!isset($_GET[$getvalue]))
{
$_GET[$getvalue] = null;
}
}
Refactorings
No refactoring yet !
Vilx-
November 16, 2009, November 16, 2009 10:15, permalink
I usually create a GET and POST wrapper functions. Something along these lines:
function GET($name, $default=null)
{
}
Vilx-
November 16, 2009, November 16, 2009 10:15, permalink
I usually create a GET and POST wrapper functions. Something along these lines:
function GET($name, $default=null)
{
}
Vilx-
November 16, 2009, November 16, 2009 10:18, permalink
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;
}
10goto10.myopenid.com
November 16, 2009, November 16, 2009 11:40, permalink
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?
Scott Reynen
November 16, 2009, November 16, 2009 13:17, permalink
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);
Tj Holowaychuk
November 16, 2009, November 16, 2009 16:54, permalink
If only PHP did not suck so much you could do this:
function GET($key, $default = null) {
return $_GET[$key] || $default;
}
Ben Marini
November 16, 2009, November 16, 2009 17:15, permalink
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() {
// ...
}
}
bob
November 16, 2009, November 16, 2009 19:32, permalink
$_GET = array_merge(array_fill_keys($getvalues, null), $_GET);
Thomas Salvador
November 16, 2009, November 16, 2009 19:39, permalink
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 );
}
Thomas Salvador
November 16, 2009, November 16, 2009 19:41, permalink
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 );
}
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 :)