It might be confusing as far as i red on many forums that fails to explain the diffrence between isset() and empty().
So let me put it this way :
Isset() checks if a variable has a value including ( Flase , 0 , or Empty string) , But not NULL.
Returns TRUE if var exists; FALSE otherwise.
On the other hand the empty() function checks if the variable has an empty value empty string , 0, NULL ,or False. Returns FALSE if var has a non-empty and non-zero value.
Comparison example on both isset() and empty()
<?php
$var = 0;
// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}
// Evaluates as true because $var is set
if (isset($var)) {
echo '$var is set even though it is empty';
}
?>
For more information and examples visit the following links.
m0o
Thanks for information. Now, I know differences between them
empty also serves a purpose for arrays quite well
to add to that isset is pretty much useless other then for sessions i find that in that case isset many times is more ideal although empty is normally fool proof
Good point there. But nobody should be confused about it. The function name itself will suggest how it will be used.
what happens if ” $var=null; ” what does return isset??
Thanks for the info I was able to implement it in my PHP script and create a single page with a HTML form and PHP code for entering it into the database. If the values are “isset” it goes into the PHP code else displays HTML form.
Inside the if loop if the values are not empty they are entered in the database else a msg is displayed “Fields cannot be blank”
My doubt is clear after read your article regarding empty() and isset() thank you.
Great advise. For my $_GET / POST / REQUEST I use this simple function to automatically check if the value is set and avoid any undefined index warnings.
//GET
function GV($key,$default=""){
return (isset($_GET[$key])) ? $_GET[$key] : $default;
}
I did a quick write up about using this function here: quick php get post request isset function
Good article. Just found another over at Tutorial Arena that takes a more in depth look at the difference between the two: PHP isset vs empty
Hope these 2 articles help someone.
The one at Tutorial Arena complements this one nicely.
thanks. very concise!
Thanks a bunch! I used to think they were the same, but saw some example texts that stated otherwise, so I searched google for why and found this.
Ahh tricksters! Thanks for clarifying this, just been going in circles for a few mins with a DB value of NULL, who would have thought! Thanks for the post
Also just to save some space and reduce duplication errors. empty() implicitly calls isset() and you don’t need both since PHP 5.1.0
// Wrong
if ($var_notdefined)
// Right
if (isset($var_notdefined))
// Right but could cause duplication errors of the same variable and long
if (isset($var_notdefined) && ! empty($var_notdefined))
// Preferred for projects requiring PHP 5.1+
if ( ! empty($var_notdefined))
See notes on __isset() http://ca3.php.net/empty
at the first time read your post, I was a little bit confused, but then it clearer. thank you ^^
I was curious if you ever thought of changing the page layout of your
website? Its very well written; I love what youve got to say.
But maybe you could a little more in the way of content
so people could connect with it better. Youve
got an awful lot of text for only having one or 2 pictures.
Maybe you could space it out better?
You do not need to lift HEAVY to get grow to be stronger.