PHP: Check if variable exist but also if has a value equal to something
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
}
Best way to test for a variable's existence in PHP; isset() is clearly broken
If the variable you are checking would be in the global scope you could do:
array_key_exists('v', $GLOBALS)
Check if a variable is undefined in PHP
You can use -
$isTouch = isset($variable);
It will return true
if the $variable
is defined. If the variable is not defined it will return false
.
Note: It returns TRUE if the variable exists and has a value other than NULL, FALSE otherwise.
If you want to check for false
, 0
, etc., you can then use empty()
-
$isTouch = empty($variable);
empty()
works for -
- "" (an empty string)
- 0 (0 as an integer)
- 0.0 (0 as a float)
- "0" (0 as a string)
- NULL
- FALSE
- array() (an empty array)
- $var; (a variable declared, but without a value)
Test for query variable exists AND ALSO is set to a particular value?
This works best:
$konami = $_GET['konami'];
if($konami == "true")
{
echo 'Hello World!';
}
else
{
header('HTTP/1.0 404 Not Found');
}
Check if php get variable is set to anything?
You can try empty
.
if (!empty($_GET['variable'])) {
// Do something.
}
On the plus side, it will also check if the variable is set or not, i.e., there is no need to call isset
seperately.
There is some confusion regarding not calling isset
. From the documentation.
A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.
and...
That means empty() is essentially the concise equivalent to !isset($var) || $var == false.
php if and isset and if variable is not set and doesnt exist
isset returs true or false. you have to do separate check for the actual value
if(
isset($loggedInfo['status']) && $loggedInfo['status']=="client" &&
isset($loggedInfo['address1']) && trim($loggedInfo['address1']) != ''
)
{
//Do something
}
How exactly does if($variable) work?
The construct if ($variable)
tests to see if $variable
evaluates to any "truthy" value. It can be a boolean TRUE
, or a non-empty, non-NULL value, or non-zero number. Have a look at the list of boolean evaluations in the PHP docs.
From the PHP documentation:
var_dump((bool) ""); // bool(false)
var_dump((bool) 1); // bool(true)
var_dump((bool) -2); // bool(true)
var_dump((bool) "foo"); // bool(true)
var_dump((bool) 2.3e5); // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array()); // bool(false)
var_dump((bool) "false"); // bool(true)
Note however that if ($variable)
is not appropriate to use when testing if a variable or array key has been initialized. If it the variable or array key does not yet exist, this would result in an E_NOTICE Undefined variable $variable
.
Related Topics
How to Use PHP Array with SQL in Operator
Multiple Index Variables in PHP Foreach Loop
How to Display Unicode Data with PHP
Switching Between Http and Https Pages with Secure Session-Cookie
How to Exclude Certains Columns While Using Eloquent
Mysql: Add Sequence Column Based on Another Field
Turn Off Display Errors Using File "Php.Ini"
How to Convert Word Smart Quotes and Em Dashes in a String
Is HTMLentities() Sufficient for Creating Xml-Safe Values
What Are .Tpl Files? PHP, Web Design
In PHP, How to Extract Multiple E-Mail Addresses from a Block of Text and Put Them into an Array
HTML Upload Max_File_Size Does Not Appear to Work
Symfony2, Dynamic Db Connection/Early Override of Doctrine Service
Intermittent PHP Abstract Class Error
How to Display Only One Country or a Specific Area in Google Maps Using the API