empty() and isset()

This is just a quick tutorial regarding the empty() and isset() functions for people that are fairly new to the world of PHP programming.

From the PHP Web site, referring to the empty() function:

Returns FALSE if var has a non-empty and non-zero value.

That’s a good thing to know. In other words, everything from NULL, to 0 to “” will return TRUE when using the empty() function.

Here is the description of what the isset() function returns:

Returns TRUE if var exists; FALSE otherwise.

In other words, only variables that don’t exist (or, variables with strictly NULL values) will return FALSE on the isset() function. All variables that have any type of value, whether it is 0, a blank text string, etc. will return TRUE.

Something else you need to know is that textareas and textboxes in forms will be sent with “” values rather than NULL values to the $_POST[] array.

Therefore, if you are trying to process a form, and you want to make sure the person entered something into the field, you are much better off checking to see if the form value is empty, than you are checking to see if it isset(). On the other hand, if you are trying to check the value of a radio button or combobox in which one of your possible values is “0”, then you should be using the isset() function.

For the first few form processors I worked on, I was consistently using the following code:

if(isset($_POST[myField]) && $_POST[myField] != "") 
{
  //YOUR CODE ///
}

A more efficient way of doing this is obviously to use:

if(!empty($_POST[myField])) 
{
   //YOUR CODE ///
}

Just a quick tip for people struggling with this. There are two main points in this article:

1) isset() and empty() are not exactly opposite of each other. They actually check for two very different things. Therefore, using !isset() is not the same as using empty(), nor is “!empty” the same as using isset(). That’s a good thing, but it’s not something you generally figure out right off the bat.

2) Forms will send blank values to the $_POST[] array instead of sending NULL values. Therefore, the variable you are usually checking from your form is, in the strictest sense of the word, “set”. However, it may still have an “empty” value. Therefore, if you really only want to check to see if the variable exists, even if it has an empty value (or if you want to check if a variable doesn’t exist), then you want to use the isset() function. However, if you want to check to see if the variable has (or doesn’t have) an empty value, then you are better off using the empty function.

Aman Kayare

Software Engineer | AWS Certified | CDAC Certified | .Net Core | Cloud Developer | Entity framework Core | AWS | SQL | JavaScript | Java | Spring boot

5 å¹´

Thank you! Sharad Sharma it's really helpful

赞
回复

要查看或添加评论,请登录

Sharad chandra Sharma的更多文章

  • Success & Failure just an Illusion

    Success & Failure just an Illusion

    Success n failure these two adjectives used in everyone life. These words are nothing more than a fake illusion.

  • CDN - Introduction

    CDN - Introduction

    CDN – you keep seeing the acronym. Maybe in URLs, maybe on landing pages, but it never quite clicked – what are Content…

  • Secrets of being good PHP developer (Don't share)

    Secrets of being good PHP developer (Don't share)

    PHP has become the most popular programming language for Web applications. Many popular websites are powered by PHP…

  • Polymorphism Not Complicated anymore in PHP…

    Polymorphism Not Complicated anymore in PHP…

    Polymorphism is a long word for a very simple concept. Polymorphism describes a pattern in object oriented programming…

  • if you want to be a professional PHP developer then you must know

    if you want to be a professional PHP developer then you must know

    There are many beginners or even experienced PHP developers who don’t bother to follow the best practices of the…

    2 条评论
  • Why Use PHP Framework ???

    Why Use PHP Framework ???

    Are you Paying Lots of money to developer for using Wordpress with lots of plugging ? Being a PHP web application…

  • 15 Signs Of Problematic Clients You’ll Meet in IT MARKET (Freelancer / Upwork)

    15 Signs Of Problematic Clients You’ll Meet in IT MARKET (Freelancer / Upwork)

    In freelancing, it’s not the number of clients you have that will make or break your freelance business, but the kind…

    1 条评论
  • Tips For Becoming A Successful Software Developer

    Tips For Becoming A Successful Software Developer

    If you are a great developer, there will always be a market for you. However, landing a job in a global, innovative…

  • Important PHP Security Tips You Should Follow

    Important PHP Security Tips You Should Follow

    What do Wikipedia, Twitter, and Facebook have in common? All of them use PHP technology. A survey and revealed that…

  • Create a Hashtag System with PHP?

    Create a Hashtag System with PHP?

    Hashtags are essentially a filter, allowing people to see the conversation associated with that word or phrase…

社区洞察

其他会员也浏览了