Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Check if variable exist but also if has a value equal to something

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

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.

like image 875
Mariz Melo Avatar asked Oct 24 '10 09:10

Mariz Melo


People also ask

How can I check if two values are same in PHP?

The comparison operator called Equal Operator is the double equal sign “==”. This operator accepts two inputs to compare and returns true value if both of the values are same (It compares only value of variable, not data types) and return a false value if both of the values are not same.

What does ?: Mean in PHP?

The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.

What is $_ GET in PHP?

PHP $_GET is a PHP super global variable which is used to collect form data after submitting an HTML form with method="get". $_GET can also collect data sent in the URL. Assume we have an HTML page that contains a hyperlink with parameters: <html> <body>

What is isset ($_ POST in PHP?

The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.


2 Answers

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 } 
like image 173
mellowsoon Avatar answered Sep 21 '22 13:09

mellowsoon


As of PHP7 you can use the Null Coalescing Operator ?? to avoid the double reference:

// $_GET['myvar'] isn't set...
echo ($_GET['myvar'] ?? '') == 'hello' ? "hello!\n" : "goodbye!\n";

// $_GET['myvar'] is set but != 'hello'
$_GET['myvar'] = 'farewell';
echo ($_GET['myvar'] ?? '') == 'hello' ? "hello!\n" : "goodbye!\n";

// $_GET['myvar'] is set and == 'hello'
$_GET['myvar'] = 'hello';
echo ($_GET['myvar'] ?? '') == 'hello' ? "hello!\n" : "goodbye!\n";

Output:

goodbye!
goodbye!
hello!

Code demo on 3v4l.org

In general, the expression

$a ?? $b

is equivalent to

isset($a) ? $a : $b

Note that in the code example it is necessary to place parentheses around $_GET['myvar'] ?? '' as == has higher precedence than ?? and thus

$_GET['myvar'] ?? '' == 'hello'

would evaluate to:

$_GET['myvar'] ?? ('' == 'hello')

which would be true as long as $_GET['myvar'] was set and "truthy" (see the manual) and false otherwise (since '' == 'hello' is false).

Precedence code demo on 3v4l.org

like image 45
Nick Avatar answered Sep 20 '22 13:09

Nick