What's different between isset($string)
and @$string
in PHP ?
This works both ways, so what's the difference?
if(isset($string))
....
if(@$string)
....
Definition and Usage 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.
The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases.
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.
isSet() function is used to check whether the given object is javascript set or not.
isset
is true
if the given variable exists and is not null
.@$var
is true
if the value of $var
is true
in a loose comparison.*!== null
and == true
matches different values.
In the case of @$var
, if $var
does not exist, an error will be generated internally and raised and its output suppressed through @
. This is a much more expensive operation, and it may trigger custom defined error handlers on the way. Do not ever use it if there's an alternative.
* A non-existent variable's value is substituted by null
, which equals false
.
Using isset($str)
is not the same as @$str
.
isset($str)
is true if $str
is set and is not null.@$str
is true if $str
is truthy.
Consider the following example:
$str = "0";
if (isset($str)) {
// This gets printed because $str is set
echo "Str is set" . PHP_EOL;
}
if (@$str) {
// This is NOT printed because $str is falsy
echo "Str is truthy" . PHP_EOL;
}
It should also be noted that @
, in addition to being a bad habit, incurs a significant performance penalty.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With