Are isset() and !== null equivalent? - php

Since isset appears to be a function (http://php.net/manual/en/function.isset.php), there might be some overhead for calling it. So I wonder if using !== null instead of isset would produce faster code while, most importantly, keeping the code's behavior exactly the same?

From PHP Manual:
isset — Determine if a variable is set and is not NULL
isset Returns TRUE if var exists and has value other than NULL, FALSE otherwise.
http://php.net/manual/en/function.isset.php
The function call overhead is so small you probably don't need to worry about it.
Read this post: Why are PHP function calls *so* expensive?
Note that isset is not a function (it has a special opcode for it), so it's faster.

What about $foo = NULL, a variable can be set, and also be null

isset is not a function, It is a Language construct in PHP, It is much faster.
isset determines if a variable is set and is not NULL.

as above isset is to check if a variable is set and is not NULL. To make sure I usually use it as
if( isset( $var ) === TRUE )
{
// Do what you want
}
This doesn't throw any unnecessary notices in PHP

Related

Alternative to isset(user_input) in php

I was wondering whether is another way to check if a variable coming from user input is set and not null, besides (the obvious choice) isset().
In some cases, we may not be using $_POST to get the value, but some similar custom function. isset() can not be used on the result of a function call, so an alternative way to perform the same check must be made. Now, isset() verifies two things:
Whether the value was set.
Whether the value is null. But there is some difference between assigning a variable the null value ( $variable = NULL; ) and getting a null value due to empty input fields. Or at least so I read.
So, is there a good way of checking both these requirements without using isset() ?
The equivalent of isset($var) for a function return value is func() === null.
isset basically does a !== null comparison, without throwing an error if the tested variable does not exist. This is a non-issue for function return values, since a) functions must exist (or PHP will exit with a fatal error) and b) a function always returns something, at least null. So all you really need to do is to check for null, no isset necessary.
I've written about this extensively here: The Definitive Guide To PHP's isset And empty.
Beyond this, it depends on what exactly you want to check:
test if a key was submitted via GET/POST: isset($_POST['key'])
test if there's a value and whether it's not == false: !empty($_POST['key'])
test if it's a non-empty string: isset($_POST['key']) && strlen($_POST['key'])
perhaps much more complex validations: filter_input
Here are some options...
PHP 7.4+ : null coalescing assignment operator
$variable ??= '';
PHP 7.0+ : null coalescing operator
$variable = $var ?? '';
PHP 5.3+ : ternary operator
isset($variable) ?: $var = '';
You can also use !empty() in place of isset()
the function !empty() works for both isset() and check whether the value of any string is not null, 0 or any empty string.
I usually prefer !empty() whenever I need to compare variable existence or in terms of its value.
The best way is isset but if you insist ... try empty() and strlen() Function to check wether it is empty or string lenghth is bigger than so many characters.
strlen() returns a number, length of the variable passed to it.
empty() checks if it has character in it or if it is null. with empty() you have to be becareful because some functions return 0 or false which is not considered empty.
if(!empty($var))....
OR
if(strlen($var)>2)...
I do it in most cases like this:
$v = my_func();
if (isset($v) and $v) {
...
}

Which is better for checking values: isset or = NULL?

I have a URL that would be like so:
http://mysite.com/index.php?somename=somevalue
I usually check that somename exists by doing:
if (isset($_GET['somename'])) {
However, I have seen a lot of people do this lately:
if ($_GET['somename'] != NULL) {
My question is, is either way better than the other?
Unlike the null check,
isset($_GET['somename']
will not throw an "undefined index" notice, so it's definitely the more preferable of the two.
array_key_exists("somename", $_GET);
would also be valid.
You may want to combine this with a null check however if you want to disallow empty values.
These statements are different.
isset() will check if a variable contains a value, whereas the second statement checks if the variable is equal to NULL or not.
In your case, use isset() to determine if $_GET['somename'] exists or not.
The best way is usually to do both. The isset checks if the variable exists, while you should also check to see if it is not NULL so that it won't break any later checks.
if(isset($_GET['somevalue']) && $_GET['somevalue'] != NULL)

In PHP, if a variable does not have a value, why does isset() still return TRUE?

Here is my code:
<?php
$ja = '';
if(isset($ja))
echo "cool!";
?>
I get a "cool!" when running this simple piece of code in my browser. I learned from php.net that
isset — Determine if a variable is set
and is not NULL
Well, in my code, I did declare the variable $ja, but I didn't add any value to it, so shouldn't it be "NULL"?
Even though '' seems like nothing, it still has a value (a NULL character at the end of the string).
isset() checks if the variable is set or not, which in the case (to ''), it is. You may want to set $ja to NULL first beforehand, instead of setting it to an empty string... or use empty() ;)
The empty string is still a value. so you did give it a value which is not null - '' is a perfectly normal string value. perhaps you want ! empty($ja)
Isset is used to tell whether a variable is set or not:
isset($notDefined) //false
$notDefined = 0;
isset($notDefined) //true
(Assuming that $notDefined hasn't been defined before)
To check whether the variable is empty you can use if(empty($var)) or if($var==0)
You did add value to $ja - you set it to an empty string. An empty string is not null.
What you may be confused with is that an empty string and null both evaluate to "false" in PHP when you cast it to Boolean.
PHP's documentation is fairly clear on usage of isset.
The isset function does determine whether or not an object has a value. "NULL" is truly the only way to give an object a value of nothing. $s = '' simply gives an output of nothing. BOOL values(true/false) says that it's yes or no... 0 simply gives the object a int value of 0.
As the name implies of the function, it checks if some variable has been set, in a sense not that it has some value, but in a sense that it has been created. I think the name could be a bit confusing so I will bring a javascript analogy. In javascript to check if the variable exists you do the following:
if (typeof(somevar) == "undefined")
alert("Sorry, the variable has not been set already")
else
alert("Congratulations, the variable has not been set")
So, what you are doing is that you are making a variable $ja, and since by doing so, the variable already exists and therefore has been set.
Hope this helps

Is this an OK test to see if a variable is set

Yesterday, I posted an answer to a question that included several (unknown to me at the time) very bad code examples. Since then, I've been looking at my fundamental knowledge of PHP that allowed me to think that such code is possible. This brings me to a question that I can't seem to find an answer to:
If I want to check for whether or not a variable has anything set, is it a valid practice to not use isset() or another helper function? here's a "for instance":
if($not_set){
//do something
} else {
//do something else
}
Rather than...
if(isset($not_set)){
//do something
} else {
//do something else
}
From the name of the variable, you can see that this variable is not set. Therefore the conditional would be false and the else portion would run. Up until now I have been using this practice, but after the posts yesterday, I now have an inkling that this is wrong.
Here's why I thought that it would be an ok practice to leave out the isset() function above. From PHP manual:
The if construct is one of the most
important features of many languages,
PHP included. It allows for
conditional execution of code
fragments. PHP features an if
structure that is similar to that of
C:
if (expr) statement
As described in the section about
expressions, expression is evaluated
to its Boolean value. If expression
evaluates to TRUE, PHP will execute
statement, and if it evaluates to
FALSE - it'll ignore it. More
information about what values evaluate
to FALSE can be found in the
'Converting to boolean' section.
And from the 'Converting to boolean section':
When converting to boolean
, the following values are considered
FALSE:
...
* the special type NULL (including unset variables)
Why would the manual go out of its way to state that unset variables are included if this is a bad practice? If it's unset, it gets converted to NULL and therefore is evaluated properly by the conditional. Using isset() will find the same result, but will take extra cycles to do so.
Have I been wrong this whole time, and if so, why? (And just how bad it is, maybe?)
If the variable is not set you get a Notice. If you use isset() you don't get a notice. So from an error reporting point of view, using isset() is better :)
Example:
error_reporting(E_ALL);
if($a) {
echo 'foo';
}
gives
Notice: Undefined variable: a in /Users/kling/test on line 5
whereas
error_reporting(E_ALL);
if(isset($a)) {
echo 'foo';
}
does not output anything.
The bottom line: If code quality is important to you, use isset().
It's okay but not good practice to use if to check for a set variable. Two reasons off the top of my head:
Using isset makes the intent clear - that you're checking whether the variable is set, and not instead checking whether a condition is true.
if ($not_set) will evaluate to false when $not_set is actually set but is equal to boolean false.
You will run in to problems if your variable is set, but evaluates to FALSE, like the following:
the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the
string "0"
an array with zero elements
an object with zero member
variables (PHP 4 only)
the special type NULL (including
unset variables)
SimpleXML objects created from empty
tags
Taken from the PHP manual.
Basically, using isset() is showing that you are explicitly checking if a variable exists and is not NULL, while the structure of your if statement only checks if the variable is true. It is more clear and less error-prone.
It is a common practise, but is not good -- you should always use isset!
If your $not_set is set, and is a bool with the value false, your "test" will fail!
isset works as a guard preventing you from using variables that do not actually exist.
if (isset($foo)) and if ($foo) do not mean the same thing. isset just tells you if the variable actually exists and if it's okay to use it, it does not evaluate the value of the variable itself*.
Hence, you should usually use one of these two patterns:
If the variable is sure to exist and you just want to check its value:
if ($foo == 'bar')
If the variable may or may not exist, and you want to check its value:
if (isset($foo) && $foo == 'bar')
If you're just interested that a variable is set and evaluates to true, i.e. if ($foo), you can use empty:
if (isset($foo) && $foo)
// is the same as
if (!empty($foo))
* it does check for null, where null is as good as not being set at all

Detecting insufficient PHP variables: FALSE vs NULL vs unset() vs empty()?

What is the best way to define that a value does not exist in PHP, or is not sufficent for the applications needs.
$var = NULL, $var = array(), $var = FALSE?
And what is the best way to test?
isset($var), empty($var), if($var != NULL), if($var)?
Initializing variables as what they will be, e.g. NULL if a string, array() if they will be arrays, has some benefits in that they will function in the setting they are ment to without any unexpected results.
e.g. foreach($emptyArray) won't complain it just wont output anything, whereas foreach($false) will complain about the wrong variable type.
But it seams like an unnecessary hassle to have so many different ways of doing basically the same thing. eg. if(empty($var)) or if ($var == NULL)
Duplicate: Best way to test for a variable’s existence in PHP; isset() is clearly broken
Each function you named is for different purposes, and they should be used accordingly:
empty: tells if an existing variable is with a value that could be considered empty (0 for numbers, empty array for arrays, equal to NULL, etc.).
isset($var): tells if the script encountered a line before where the variable was the left side of an assignment (i.e. $var = 3;) or any other obscure methods such as extract, list or eval. This is the way to find if a variable has been set.
$var == NULL: This is tricky, since 0 == NULL. If you really want to tell if a variable is NULL, you should use triple =: $var === NULL.
if($var): same as $var == NULL.
As useful link is http://us2.php.net/manual/en/types.comparisons.php.
The way to tell if the variable is good for a piece of script you're coding will entirely depend on your code, so there's no single way of checking it.
One last piece of advice: if you expect a variable to be an array, don't wait for it to be set somewhere. Instead, initialize it beforehand, then let your code run and maybe it will get overwritten with a new array:
// Initialize the variable, so we always get an array in this variable without worrying about other code.
$var = array();
if(some_weird_condition){
$var = array(1, 2, 3);
}
// Will work every time.
foreach($var as $key => $value){
}
Another thing to remember is that since php is liberal in what it allows to evaluate to NULL or empty, it's necessary to use the identity operators (===, !== see http://php.net/operators.comparison. This is the reason why all of these comparison and equality functions exists, since you often have to differentiate between values with subtle differences.
If you are explicitly checking for NULL, always use $var === NULL
My vote goes for unset(), because non existing variables should generate an notice when used.
Testing gets a bit more complicated, because isset() wil be false if the variable is "not existings" or null. (Language designflaw if you'd ask me)
You could use array_key_exists() for valiables in $GLOBALS, $_POST etc.
For class properties there is property_exists().

Categories