Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

engrlaravel's avatar

How to check if variable is empty/null/NULL/not set/0

I know there are many ways like

empty($var)
is_null($var)
isset($var)
if(!$var)

But each one has different purpose. I want to have a combined function in laravel to check all these

Does laravel provide a single function to check all in one?

Need to check all possible like

NULL,null,'',0, undefined , not posted and others
0 likes
6 replies
tisuchi's avatar

@engrlaravel

If this data comes from the user form, you can define everything in form validation part. It will be easier for you to handle.

2 likes
petrit's avatar

Regarding the php documentation, the following values are considered to be empty:

- "" (an empty string)
- 0 (0 as an integer)
- 0.0 (0 as a float)
- "0" (0 as a string)
- NULL
- FALSE
- array() (an empty array)

And isset() function may be used for undefined variables. But in Laravel you have to be careful when dealing with collections.

engrlaravel's avatar

@tisuchi : some time it comes from user form, some time in my function. i want to do it in php server side, it is not validation.

is there any function?

tisuchi's avatar

@engrlaravel

There are probably two options I can think of right now.

  1. You need to write a regular expression then check whether it return value or not. In that case, you can easily handle without using many built-in functions.

  2. By somehow if you need to use built-in functions, I believe you have to use at least two functions to cover all . of your criteria.

Now choose is yours, which one you will pick up.

1 like

Please or to participate in this conversation.