Igor Petev
Igor Petev

Reputation: 607

How to check in PHP if variable is set and check if two values match set variable?

I know that is basic question, but i can't figure out how to test if $_GET['zanr'] contains '' or 'sve' to do other stuff in my script...i try using this code but it only checks '' and 'sve' is ignored...so how to check if 'zanr' contains '' or 'sve' do stuff a else do stuff b?

if (isset($_GET['zanr']) === '' || isset($_GET['zanr']) === 'sve'){
  echo "zanr = '' or 'sve'";
} else {
  echo "zanr contains other values...";
}

I also try using ?? but no success...thanks.

Upvotes: 2

Views: 1554

Answers (2)

Sandeep K.
Sandeep K.

Reputation: 759

Since your goal is to check if your $_GET is empty, use PHP built in function: empty(). And your second statement is wrong because isset() is returning a boolean and therefore you're not validating the string itself. So be sure to remove isset() and just compare if $_GET['zanr'] has your specific string.

Use this:

if (empty($_GET['zanr']) || $_GET['zanr'] == 'sve'){
    echo "zanr = '' or 'sve'";
} else {
    echo "zanr contains other values...";
}

Upvotes: 3

TrickStar
TrickStar

Reputation: 229

Try below code.

if (isset($_GET['zanr']) && ($_GET['zanr'] == '' || $_GET['zanr'] == 'sve')){
  echo "zanr = '' or 'sve'";
} else {
  echo "zanr contains other values...";
}

Upvotes: 1

Related Questions