Cameron
Cameron

Reputation: 28853

Test for query variable exists AND ALSO is set to a particular value?

I want to check if a query variable exists or not. Then I would do something based on that query value. If it exists and is true, do something. If it doesn't exist or is false, do something else such as show a 404 page.

e.g If the url was domain.com?konami=true

if (condition) {
    //something
} else {
    //show404
}

Upvotes: 2

Views: 3929

Answers (10)

r3wt
r3wt

Reputation: 4742

if(!$variable) { 

     //the variable is null
     die("error, $variable is null"); 
}else{

     //the variable is set, your code here.
    $db->query("....");
}

Upvotes: 0

Tomas
Tomas

Reputation: 59555

There is a little confusion around what value should be tested. Do you want to test the konami parameter for being true in the sense of boolean, i.e. you want to test konami parameter for being truthy, or do you want to test if it has string value equal to "true"? Or do you want to test konami parameter for any value in general?

I guess what is wanted here is to test konami for a given string value, "true" in this case, and for being set at the same time. In this case, this is perfectly enough:

ini_set('error_reporting', E_ALL & ~E_NOTICE);
...
if ($_GET['konami'] == "true")
    ...

This is enough, because if the $_GET['konami'] is unset, it cannot be equal to any string value except for "". Using === is not neccessary since you know that $_GET['konami'] is string.

Note that I turn off the E_NOTICE which someone may not like - but these type of "notices" are normally fine in many programming languages and you won't miss anything if you disable them. If you don't, you have to make your code unecessarily complex like this:

if (isset($_GET['konami']) && $_GET['konami'] == "true")

Do you really want to complicate your code with this, or rather make it simple and ignore the notices like Undefinex index? It's up to you.

Problems with other answers as you mentioned:

  • @Jan Hancic answer: it tests for true, not "true".
  • @Cameron answer: might be simplified and he didn't mention the necessity of disabling E_NOTICE.
  • @Frits van Campen's answer: too complex to my taste, unnecessary test for === true

Upvotes: 4

Ajitha Ms
Ajitha Ms

Reputation: 555

You may try this code. In this code checked two conditions by one if condition that is $konami contains value and $konami contains 'true'.

$konami = $_GET['konami'];
if( ($konami) && ($konami == "true")){
/*enter you true statement code */
}else {
/* enter your false statement code */
} 

Upvotes: 1

manta
manta

Reputation: 1688

quick and simple.

$konami = filter_input(INPUT_GET, 'konami', FILTER_VALIDATE_BOOLEAN) or die();

ref:

filter flags

filter_input

Upvotes: 1

Jan Hančič
Jan Hančič

Reputation: 53929

Umm this?

if (isset($_GET['konami']) === true) {
    // something
} else {
    //show 404
}

Upvotes: 3

Halcyon
Halcyon

Reputation: 57703

OPs question is a bit unclear. If you assume that he wants to check that konami is a $_GET parameter and that it has the value of "true" do:

if (isset($_GET["konami"]) === true && $_GET["konami"] === "true") {
    // something
} else {
    // show 404
}

The problem with the current accepted answer (by Cameron) is that it's lacking the isset check (which is unforgivable, it is objectively wrong). The problem of the highest voted answer (by Jan Hancic) is that it lacks the === "true" check (which is debatable, it depends on how your interpret the question).

Note that && is a lazy-and, meaning that if the first part is false, the second part will never be evaluated, which prevents the "Undefined index" warning. So the order of the statements is important.

Also note that $a === true is not the same as $a === "true". The first compares a boolean whereas the second compares a string.

If you do weak comparison $a == true you are checking for truthy-ness.

Many values are truthy, like the string "true", the number 1, and the string "foo". Examples of falsy values are: empty string "", the number 0 and null.

"true" == true;   // true
"foo" == true;    // true
1 == true;        // true
"" == true;       // false
0 == true;        // false
null == true;     // false

"true" === true;  // false
"true" === false; // false

Upvotes: 8

prinsen
prinsen

Reputation: 975

Easiest and shortest way of doing it:

if($konami != null){ echo $konami; } else { header('HTTP/1.0 404 Not Found'); }

Upvotes: -1

Floris
Floris

Reputation: 11

Easy:

if(isset($_GET['konami']) && $_GET['konami'] != 'false') {
     //something
} else {
     // 404
}

Upvotes: 1

lorem monkey
lorem monkey

Reputation: 3992

You can do it like this:

$konami = false;
if(isset($_GET['konami'])){
    $konami = $_GET['konami'];
}

if($konami == "true"){
    echo 'Hello World!';
}
else{
    header('HTTP/1.0 404 Not Found');
}

In this case you'll always have $konami defined and - if set - filled with the value of your GET-parameter.

Upvotes: 0

Cameron
Cameron

Reputation: 28853

This works best:

$konami = $_GET['konami'];

if($konami == "true")
{
    echo 'Hello World!';
}
else
{
    header('HTTP/1.0 404 Not Found');
}

Upvotes: -1

Related Questions