Harea Costicla Harea Costicla - 1 year ago 44
PHP Question

Compare 2 differents conditions with the same idea

I have a question : what is the difference between :

$value !=''

Answer Source

The main difference in practical use between $value != '' and !isset($value) is that you can use isset() like this:

    echo '$value is not declared or is equal to null';

If $value is not actually set then you won't get a notice with isset(). If you did something like this:

if($value != ''){
    echo $value." is not equal to nothing"; 

If $value is not set then this will cause a notice in PHP explaining that $value has not yet been declared.

The thing to note is that isset() will check that the variable has been declared but will not check that it isn't equal to ''.

But what about empty()?

The other part of the puzzle here would be empty() which will check whether a variable is declared and is not equal to ''. You could do something like this:

    echo 'We know that $value is declared and not equal to nothing.';

which would be the same as doing this:

if(isset($value) && $value != ''){
    echo 'We know that $value is declared and not equal to nothing.';

Further reading:

isset() - PHP docs

empty() - PHP docs

isset, empty checks - similar question

Why check both isset and empty?

All of these examples work in PHP 5.3+