Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's different between "isset($string)" & "@$string" on PHP?

Tags:

php

What's different between isset($string) and @$string in PHP ?

This works both ways, so what's the difference?

if(isset($string))
 ....


if(@$string)
 ....
like image 603
Abudayah Avatar asked Jul 23 '15 13:07

Abudayah


People also ask

What is an isset () function?

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.

What is isset ($_ GET in PHP?

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.

What can I use instead of isset in PHP?

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.

Is there Isset in JS?

isSet() function is used to check whether the given object is javascript set or not.


2 Answers

  1. 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.

  2. 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.

like image 169
deceze Avatar answered Oct 07 '22 19:10

deceze


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.

like image 24
Mr. Llama Avatar answered Oct 07 '22 18:10

Mr. Llama