views:

100

answers:

4

PHP Question:

Hi there, this is just something I was wondering today, what I'm doing:

I have (or not) a variable $_GET['myvar'] coming from my querystring and I want to check if this variable exists and also if the value corresponds to something inside my if statment:

What I'm doing and think is not the best way to do:

if(isset($_GET['myvar']) && $_GET['myvar'] == 'something'): do something

My question is, exist any way to do this without declare the variable twice?

that is a simple case but imagine have to compare many of this $myvar variables...

Thanks everyone.

A: 

My question is, exist any way to do this without declare the variable twice?

No, there is no way to do this correctly without doing two checks. I hate it, too.

One way to work around it would be to import all relevant GET variables at one central point into an array or object of some sort (Most MVC frameworks do this automatically) and setting all properties that are needed later. (Instead of accessing request variables across the code.)

Pekka
Just define all your variables. That's the point of all that mess.
Col. Shrapnel
@Col yeah. ` ` ` `
Pekka
Thank you Pekka, is really very boring do that.
Mariz Melo
Sometimes in a big system is difficult predict when the variable will appear, that why declare the variable may not help. But you're right in most of the cases.
Mariz Melo
@Mariz I disagree: It should never be difficult to predict when the variable will appear: If that is the case, you have bad code.
Pekka
Pekka this is the "ideal", but that is difficult in a rush world with people from different background working in the same project. Anyway thanks people.
Mariz Melo
A: 

Sadly that's the only way to do it. But there are approaches for dealing with larger arrays. For instance something like this:

$required = array('myvar', 'foo', 'bar', 'baz');
$missing = array_diff($required, array_keys($_GET));

The variable $missing now contains a list of values that are required, but missing from the $_GET array. You can use the $missing array to display a message to the visitor.

Or you can use something like that:

$required = array('myvar', 'foo', 'bar', 'baz');
$missing = array_diff($required, array_keys($_GET));
foreach($missing as $m ) {
    $_GET[$m] = null;
}

Now each required element at least has a default value. You can now use if($_GET['myvar'] == 'something') without worrying that the key isn't set.

Update

One other way to clean up the code would be using a function that checks if the value is set.

function getValue($key) {
    if (!isset($_GET[$key])) {
        return false;
    }
    return $_GET[$key];
}

if (getValue('myvar') == 'something') {
    // Do something
}
mellowsoon
Well I saw this around there, just hoping be possible without using arrays, thanks.
Mariz Melo
Updated my answer to show a possible way to do this without using another array.
mellowsoon
Thank you for the code Mellowsoon, back to work now.
Mariz Melo
A: 

Well, you could get by with just if($_GET['myvar'] == 'something') since that condition presumes that the variable also exists. If it doesn't, the expression will also result in false.

I think it's ok to do this inside conditional statements like above. No harm done really.

DanMan
And it fires a notice, if the myvar index doesn't exist.
erenon
True, but you're just testing for that index. IMHO it would only be really bad, if that if-clause didn't exist at all. A notice i can live with. After all, it's just a notice, which by definition are harmless (usually).
DanMan
Oof, I couldn't disagree more. If your goal is to create a sloppy PHP codebase full of sneaky/silent bugs, ignoring notices would be a great way to start. You should always develop with notices turned on, and you should always treat them as bugs to be fixed.
beamrider9
A: 

Thanks Mellowsoon and Pekka, I did some research here and come up with this:

  • Check and declare each variable as null (if is the case) before start to use (as recommended):
!isset($_GET['myvar']) ? $_GET['myvar'] = 0:0;

*ok this one is simple but works fine, you can start to use the variable everywhere after this line

  • Using array to cover all cases:
$myvars = array( 'var1', 'var2', 'var3');
foreach($myvars as $key)
    !isset($_GET[$key]) ? $_GET[$key] =0:0;

*after that you are free to use your variables (var1, var2, var3 ... etc),

PS.: function receiving a JSON object should be better (or a simple string with separator for explode/implode);

... Better approaches are welcome :)


UPDATE:

Use $_REQUEST instead of $_GET, this way you cover both $_GET and $_POST variables.

!isset($_REQUEST[$key]) ? $_REQUEST[$key] =0:0;
Mariz Melo