Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

php - What's the difference between if(!$variable) and if(isset($variable))?

What is the difference between if(!$variable) and if(isset($variable))?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well, the answer is pretty simple. isset($var) returns whether or not a variable exists and is not null, where !$var tells you if that variable is true, or anything that evaluates to true (such as a non-empty string). This is summarized in the first table of this documentation page.

Also, using !$var will output a notice that you're using an undefined variable, whereas isset($var) won't do that.

Mind you, they are two different things:

<?php
var_dump( isset($foo) ); // false.
var_dump( !$foo );       // true, but with a warning.

$foo = false;
var_dump( isset($foo) ); // true
var_dump( !$foo );       // true.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...