0

Just a simple php question, i have this:

if(isset($_GET['i'])) { if($_GET['i']=='dash') {

It is possible to use one IF? Thanks!

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
DomingoSL
  • 14,920
  • 24
  • 99
  • 173

3 Answers3

5

Use a logical operator.

if (isset($_GET['i']) && $_GET['i'] == 'dash') {

This will test that both conditions return true in the test.

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
  • Just to clarify, `&&` means `and`, you could use `||` for `or` if you wanted either condition to match. – Levi Morrison Oct 22 '11 at 16:02
  • What you might be asking is whether the `if($_GET['i']=='dash')` will throw an error if `$_GET['i']` is not set; it won't. PHP uses what's called "lazy evaluation," so if `$_GET['i']` is not set, PHP knows the entire if statement will fail so it will not evaluate `$_GET['i'] == 'dash'`. – toon81 Oct 22 '11 at 16:04
  • By the way if the first condition returns `false`, the second one will never be tested! So you simply can access `$_GET['i']` after you tested the existence in the IF. – ComFreek Oct 22 '11 at 16:05
2

I do it this way:

function initGet($var, $default=''){
    if(!isset($_GET[$var])) return $default;
    return $_GET[$var];
}

So you can check it easy:

if(initGet('i')=='dash'){

And you could add a default value if needed:

if(initGet('i', 'dash')=='dash'){
     // would be true if $_GET['i'] is not set
Marc
  • 6,749
  • 9
  • 47
  • 78
1

You can alternatively use array_intersect_assoc to check both conditions at once:

if (array_intersect_assoc($_GET, array("i" => "dash"))) {
mario
  • 144,265
  • 20
  • 237
  • 291