Free cookie consent management tool by TermsFeed Generator PHP isset and empty | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP isset and empty

PHP isset and empty

Last Updated on Mar 21, 2023

What are they?

isset and empty are two functions that help us check the existence of a value. They are way more useful than you think.

isset

isset checks if a value has been set and it’s not null. If it’s undefined then it will return false otherwise it will return true. for example:

$twitter = 'Pratham';
isset($twitter);    // true
isset($css);        // false

isset($css) returns false because it has not been defined at all.

empty

empty checks if a value has been set and it’s not undefined like isset function. But it also checks if the value is not a false value.

For example an empty array or 0 or empty string. All of them would be considered as empty.

$twitter = 'Pratham';
$js = null;
$web3 = 0;
$eth = false;
empty($twitter);    // false
empty($css);        // true
empty($js);             // true
empty($web3);       // true
empty($eth);        // true

Opposite

Since empty and isset both return a value you can use ! to check for the opposite.

For example if we want to say check if $twitter is not empty Or check if $twitter is not set we can write:

If (!empty($twitter)){

}
Or check if $twitter is not set
If (!isset($twitter)){

}

Use case

So when do we use it?

We use it a lot.

For example let’s say we have a form and user has submitted the form and in the backend we are checking if the username and password has been submitted

We want to say check if user is not empty and password is not empty

if(!empty($username) && !empty($password)){
    // do something
}

https://youtu.be/mZXXYjPSkdU

Conclusion

Now you know about isset and empty functions in PHP.

I recommend you to open a PHP files and try define multiple variables. then try to check the return value of empty and isset on those variables.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • isset in PHP
  • empty in PHP

 

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP Date and Time
Mar 21, 2023 programming

PHP Date and Time

Today we are going to learn about date and time in PHP. knowing how to work with dates and times and how to work with different formats of dates is very important. ...

14 Min Read Read More
PHP Database
Mar 22, 2023 programming

PHP Database

Today we are going to talk about working with a database in PHP. working with databases is a very important part of web applications. you should be able to store data and read them. ...

14 Min Read Read More
PHP calculate distance between 2 places in 9 easy steps
Feb 14, 2023 programming

PHP calculate distance between 2 places in 9 easy steps

Today we are going to talk about calculating the distance between two place in PHP in a few easy steps. ...

7 Min Read Read More
A Programmer's Guide to Debugging: Essential Steps to Follow
Mar 23, 2024 programming

A Programmer's Guide to Debugging: Essential Steps to Follow

Debugging is a crucial skill for any programmer, as it helps identify and fix issues in the code. Effective debugging not only improves the overall quality of your software but can also save you time and frustration. ...

10 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...