views:

114

answers:

7

I am getting tried of if isset($_GET['whatever'])... before the rest of my if statement. E_NOTICE errors are way to handy to turn off and for $_POST variables I have a solution in my init script..

$POST = (is_array( $_POST ) && count( $_POST ) > 0);

I find this helps self posting scripts look clean.

if ($POST) {
    // now do something with $_POST...
}

I'm not sure how to dynamically do this if you have no idea what the key is? Can anyone help find a similar solution for $_GET variables?

EDIT:

I simply want if ($_GET['whatever'] == "whatever"); to return false if it's not set and no E_NOTICE errors.

EDIT:

Sorry if I'm unclear I'm looking for a solution for $_GET not $_POST--I only am using $_POST as an example of what I hope to achieve.

+1  A: 

The main idea of all that mess is simple: every variable in your program should be initialized before use.
Thus, the best you can do is to set all variables centralized, say, at the very top of your script.

But there are some different cases. For the case you posted, you need none of these but

if ($_SERVER['REQUEST_METHOD'] == "POST")
Col. Shrapnel
sorry, I may be unclear.. I have a solution for $_POST but need one for $_GET
Mikey1980
+1 for the `every variable in your program should be initialized before use.`
robertbasic
@Mikey1980 actually you don't have a [proper] solution for $_POST and you don't have [the same] problem with $_GET array itself, as it's already always defined and is array.
Col. Shrapnel
@Col. Shrapnel: thanks for the edu, but doesn't help
Mikey1980
@Mikey1980 it is not true as well just learned to phrase your questions properly.
Col. Shrapnel
+3  A: 

Sometimes I use a global GET function:

function GET($key, $default = null) {
    return isset($_GET[$key]) ? $_GET[$key] : $default;
}
Ionuț G. Stan
Awesome.. THANKS! this'll make the code a lot easier to read.
Mikey1980
A: 

You can make a function to do this for you.

function getVar($item){
    return isset($_GET[$item]) ? $_GET[$item] : '';
}

Then you can do if(getVar('whatever') == "whatever").

Rocket
A: 

When writing these sorts of scripts, I usually do something like this at the top:

$submitted = isset($_POST['submit']) ? true : false;
$username = isset($_POST['username']) ? $_POST['username'] : null;
$password = isset($_POST['password']) ? $_POST['password'] : null;
(etc.)

Then, when testing the values, it looks something like this:

if ($submitted)
{
    if ($username && $password)
    {
        // do stuff with username and password including testing their values
    }
}

It seems to work pretty well for me.

Ryan Kinal
you are writing one line of code to check for every variable, whether it is set or not, why not call a function like others have suggested?
sandeepan
A: 

simple I think you want to check if get value is set then need to do the next step otherwise do different

I like the first answer but need to check thats if it works for you

if(isset($_GET['whatever']){ if ($_GET['whatever'] == "whatever");{ //do the rest } else{ //do the rest } }

hope it may help if what I think is your need

Rahul TS
+1  A: 

Here are two methods.

/*
* Makes any request keys into variables of the same name
*/
foreach($_GET AS $key => $value) {
    ${$key} = ($value);
}    

//Assuming a input key of $_GET['whatever'] 
echo $whatever;

/*
* Casting to an object
*/
$get = (object) $_GET;

//Assuming a input key of $_GET['whatever'] 
echo $get->whatever

A third option that I can think of is making the $_GET into its own class. PHP overloading is handy trick for doing this.

http://php.net/manual/en/language.oop5.overloading.php

I didn't feel like writing up an example demonstrating this. :)

LLBBL
I always wondered how to dynamically create variable names.. awesome job
Mikey1980
Thanks! I would also recommend that in the first example you call some sort of sanitization class from inside the foreach, while in the second example you would just do it before casting to an object.
LLBBL
+1  A: 

Already answered, but since it wasn't mentioned, also try:

$var = filter_input(INPUT_GET, 'varname');

That will return null if it doesn't exist, without the annoying array notices, and also filters out potential bad stuff in various ways. See also: http://www.php.net/filter

tadamson
thanks, I'm gonna have to play with that one a bit isn't clear how I'd use that in an if statement.. none the less I am curious to learn that fn
Mikey1980