Question

How do I check if $_GET superglobal variable is set and when it doesn't equal 0-100-euro?

Unsuccessful example:

if( !isset($_GET['preis'] ) AND $_GET['preis'] === "0-100-euro" );
Was it helpful?

Solution 4

$_GET['preis'] != "0-100-euro"

Right now, you try to see if the variable does not exist AND if it contains "0-100-euro". It will always return false. Just change == by !=.

OTHER TIPS

I think you're looking for this:

if($_GET['preis'] !== "0-100-euro") {
    echo "your message";
}

I think that it is better to check if a key exists, because if you use "isset()" PHP can return a notice message: "Undefined index".

<?php

if (!array_key_exists('preis', $_GET) || $_GET['preis'] !== '0-100-euro') {
    die('"preis" is not set or is equal to "0-100-euro".');
}

Use OR, not AND, and reverse the test of the value.

if (!isset($_GET['preis') || $_GET['preis' != '0-100-euro')

Don't they teach deMorgan's Law any more?

You could do that by using sleek shortcuts:

isset( $_GET['preis'] ) && ( isset($_GET['preis'] ) && $_GET['preis'] != "0-100-euro" ) ? $result = true : $result = false;

Later you can check your request and in case it's not what you wanted, it will print:

isset( $result ) && print ( "0-100-euro is not set" );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top