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

the ultimate guide to Git: learn everything you need in less than 1 hour
Nov 28, 2022 programming

the ultimate guide to Git: learn everything you need in less than 1 hour

Here is everything you need to know about git in a series of short question and answers. ...

11 Min Read Read More
PHP Constant
Mar 22, 2023 programming

PHP Constant

Today we are going to talk about constants in PHP. Constants are like variables that can’t be changed. Hence the name! it’s constant and it’s not variable. they are a good way to store information. ...

4 Min Read Read More
3 Ways to add Conditional and Loop within HTML
Mar 21, 2023 programming

3 Ways to add Conditional and Loop within HTML

Today we are going to talk about different ways we can add PHP conditionals and loops within HTML . ...

9 Min Read Read More
TypeScript Beginner to Advanced 2025: The Complete Guide
Jul 18, 2025 programming

TypeScript Beginner to Advanced 2025: The Complete Guide

TypeScript is no longer optional for serious JavaScript developers, it’s essential. Whether you're building modern front-end applications with React, scaling backend services with Node.js, or contributing to large open-source projects, TypeScript is the industry standard in 2025. ...

23 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 ...