empty vs isset vs is_null in PHP

empty vs isset vs is_null in PHP

When working with variables in PHP, it’s important to know the difference between the isset(), empty(), and is_null() functions. These functions are commonly used to determine whether a variable has a value or not. In this tutorial, you will learn difference between PHP empty() vs isset() vs is_null() function with its definition, syntax, require parameters and with examples.

isset vs empty vs is_null in PHP

Let’s see the isset(), empty(), and is_null() along with their definition, syntax, usage, and difference:

  • isset() function
  • empty() function
  • is_null() function

isset() function

isset() is a inbuilt function of PHP. which is used to test/check if a variable value is set or not. This function returns the result as a boolean form (TRUE / FALSE).

Note:- If the variable value is set, this function will return the boolean value true. Otherwise, this function will return the boolean value false.

Syntax:

isset(variable, ….);

Parameters | isset() function

The PHP isset() function will accept multiple variables as shown in the above-given syntax of isset() function.

Note: You can pass more than one variable in this function, but this function will return true only if all of the variables are set.

Example PHP isset() function

Let’s take an example of a function isset(). Here, we have two variables. The first name is variable and the second is one name is variable1. We will first check and test it with isset() set the value in the variable. And also we will not set value of the second variable, we will check with an isset() function.

You can see the example below: “

<?php 


// PHP code to check without set variable value
$variable; 

if( isset( $variable ) ) { 
	print_r(" The variable value is set, checked by isset() "); 
}else{
	print_r(" The variable value is not set, checked by isset() "); 
}

echo "<br>";

// PHP code to check with set variable value

$variable2 = 'hello world';

if( isset( $variable1 ) ) { 
	print_r(" The variable value is set, checked by isset() "); 
}else{
	print_r(" The variable value is not set, checked by isset() "); 
}

?> 

empty() function

empty() is a inbuilt function of PHP. and commonly is to test/check if a given variable value is empty or not. This function returns the result as a boolean form (TRUE / FALSE).

Note:- If the variable value is not empty, this function will return the boolean value false. Otherwise, return the boolean value true.

Syntax:

empty( $variable )

Parameters | empty() function

The empty() PHP function accepts one parameter only. You can see the syntax of this variable above.

Example of empty() function

In the example below, we have two variables, the first is num1 and the second is num2. We will first check and test it with empty() giving the value in the variable. And also we will set the null value of the second variable, we will check with an empty function.

You can see the example below:

<?php 


// PHP Code to check first variable

$variable; 

if( empty( $variable ) ) { 
	print_r(" The variable value is empty, checked by empty() "); 
}else{
	print_r(" The variable value is not empty, checked by empty() "); 
}

echo "<br>";


// PHP Code to test variable with empty()

$variable2 = 'PHP world!'; 

if( empty( $variable2 ) ) { 
	print_r(" The variable value is empty"); 
  
}else{
	print_r(" The variable value is not empty"); 
	
} 


?> 

is_null() function

is_null() function is an inbuilt PHP function. Which is used to find / test whether a variable value is NULL or NOT. This function returns the result as a boolean form (TRUE / FALSE).

Note:- If the variable value is null, this function will return the boolean value true. Otherwise, this function returns the boolean value false.

Syntax:

is_null( $variable )

Parameters | is_null() function

The is_null() PHP function accepts one parameter only. You can see the syntax of this variable above.

Example of is_null() function

In the example below, we have two variables, the first is variable and the second is variable1. We will set the null value of the variable, we will check with an is_null() function. Also, We will first check or test it with is_null() giving the value in the variable2.

You can see the example below:

<?php 


// PHP Code or scritp to explain php is_null() function

$variable = null; 

if( is_null( $variable ) ) { 
	print_r(" The variable value is null "); 
}else{
	print_r(" The variable value is not null"); 
}

echo "<br>";

// PHP Code to test variable with is_null()

$variable2 = '10';

if( is_null( $variable2 ) ) { 
	print_r(" The variable value is null "); 
}else{
	print_r(" The variable value is not null"); 
}


?> 

Uses of these functions:

 Functions“”“apple”NULLFALSE0undefined
empty()TRUEFALSETRUETRUETRUETRUE
is_null()FALSEFALSETRUEFALSEFALSEERROR
isset()TRUETRUEFALSETRUETRUEFALSE

Question 1 :- what is the difference between is_null() and isset()?

Answer:-

You can see the difference between empty() and is_null() function:

isset() function is_null() function
it returns true only when the variable is not null. it returns true only when the variable is null.

Question 2:- What is the difference between is_null() and isset()?

Answer:-

The main difference between isset and empty in php is that, You can see below

isset() function empty() function
it returns true only when the variable is not null. it will return true if the variable is an empty string,0, NULL ,or False value.

Recommended PHP Posts:

  1. Autocomplete Search Box in PHP MySQL
  2. Compare Arrays PHP | PHP array_diff() Function
  3. Get, Write, Read, Load, JSON File from Url PHP
  4. Functions: Remove First Character From String PHP
  5. Remove Specific/Special Characters From String In PHP
  6. How to Replace First and Last Character From String PHP
  7. Reverse String in PHP
  8. Array Push, POP PHP | PHP Array Tutorial
  9. PHP Search Multidimensional Array By key, value and return key
  10. json_encode()- Convert Array To JSON | Object To JSON PHP
  11. PHP remove duplicates from multidimensional array
  12. PHP Remove Duplicate Elements or Values from Array PHP
  13. Get Highest Value in Multidimensional Array PHP
  14. PHP Get Min or Minimum Value in Array
  15. Convert CSV to JSON PHP – PHP Tutorial
  16. PHP String to Uppercase, Lowercase & First Letter Uppercase

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *