Difference between isset() and empty() Functions - GeeksforGeeks (2024)

Skip to content

Difference between isset() and empty() Functions - GeeksforGeeks (1)

Last Updated : 23 Sep, 2021

Improve

Improve

Like Article

Like

Save

Report

The empty() function is an inbuilt function in PHP that is used to check whether a variable is empty or not.

These values are considered to be empty values:

  • “” ( 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)

Example: Below example illustrate the empty() function in PHP.

PHP

<?php

// PHP code to demonstrate the working

// of empty() function

$var1 = 0;

$var2 = 0.0;

$var3 = "0";

$var4 = NULL;

$var5 = false;

$var6 = array();

$var7 = "";

// For value 0 as integer

empty($var1) ? print_r("True\n") : print_r("False\n");

// For value 0.0 as float

empty($var2) ? print_r("True\n") : print_r("False\n");

// For value 0 as string

empty($var3) ? print_r("True\n") : print_r("False\n");

// For value Null

empty($var4) ? print_r("True\n") : print_r("False\n");

// For value false

empty($var5) ? print_r("True\n") : print_r("False\n");

// For array

empty($var6) ? print_r("True\n") : print_r("False\n");

// For empty string

empty($var7) ? print_r("True\n") : print_r("False\n");

// For not declare $var8

empty($var8) ? print_r("True\n") : print_r("False\n");

?>

Output

TrueTrueTrueTrueTrueTrueTrueTrue

isset() Function: The isset() function is an inbuilt function in PHP that is used to determine if the variable is declared and its value is not equal to NULL.

Parameters: This function accepts one or more parameters as mentioned above and described below.

  • $var: It contains the variable which needs to check.
  • $…: It contains the list of other variables.

Return Value: It returns TRUE if var exists and its value is not equal to NULL and FALSE otherwise.

Example 2: Below examples illustrate the isset() function in PHP:

PHP

<?php

$str = "GeeksforGeeks";

// Check value of variable is set or not

if(isset($str)) {

echo "Value of variable is set";

}

else {

echo "Value of variable is not set";

}

$arr = array();

// Check value of variable is set or not

if( !isset($arr[0]) ) {

echo "\nArray is Empty";

}

else {

echo "\nArray is not empty";

}

?>

Output

Value of variable is setArray is Empty

PHP program using both isset() and empty() functions:

PHP

<?php

// PHP function to demonstrate

// isset() and !empty() function

// initialize a variable

$num = '0';

// Check isset() function

if( isset ( $num ) ) {

print_r( $num . " is set with isset function");

}

// Display new line

echo "\n";

// Initialize a variable

$num = 1;

// Check the !empty() function

if( !empty ( $num ) ) {

print_r($num . " is set with !empty function");

}

?>

Output:

0 is set with isset function1 is set with !empty function

Difference between isset() and empty() function:

isset() Function

empty() Function

The isset() function is an inbuilt function in PHP that is used to determine if the variable is declared and its value is not equal to NULL.The empty() function is an inbuilt function in PHP that is used to check whether a variable is empty or not.
The isset() function will generate a warning or e-notice when the variable does not exists.The empty() function will not generate any warning or e-notice when the variable does not exists.


Please Login to comment...

Similar Reads

Why to check both isset() and !empty() function in PHP ?

isset() Function The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases. Syntax: bool isset( $var, mixed ) Parameters: This function a

3 min read

Difference between isset() and array_key_exists() Function in PHP

isset() function The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases. Syntax: bool isset( $var, mixed ) Parameters: This function a

2 min read

PHP | isset() Function

The isset() function is an inbuilt function in PHP which is used to determine if the variable is declared and its value is not equal to NULL. Syntax: bool isset( mixed $var [, mixed $... ] ) Parameters: This function accept one or more parameter as mentioned above and described below: $var: It contains the variable which need to check. $...: It con

2 min read

PHP | IntlCalendar isSet() Function

The IntlCalendar::isSet() function is an inbuilt function in PHP which is used to check whether a given field is set or not. This function is opposite to IntlCalendar::clear() function. Syntax: Object oriented style bool IntlCalendar::isSet( int $field ) Procedural style bool intlcal_is_set( IntlCalendar $cal, int $field ) Parameters: This function

1 min read

Node.js util.types.isSet() Method

The util.types.isSet() method of the util module is primarily designed to support the needs of Node.js own Internal APIs. It is used to check whether the passed instance in the method is a built-in Set instance or not. Syntax: util.types.isSet( value ) Parameters: This method accepts a single parameter value that holds any value i.e instance of any

2 min read

Underscore.js _.isSet() Function

Underscore.js is a library in javascript that makes operations on arrays, string, objects much easier and handy. _.isSet() function is used to check whether the given object is javascript set or not. When linking the underscore.js CDN The "_" is attached to the browser as global variable. Syntax: _.isSet(object); Parameters: object: It is any JavaS

2 min read

Lodash _.isSet() Method

Lodash _.isSet() method is used to find whether the given value is classified as a Set object or not. It returns True if the given value is a set. Otherwise, it returns false. Syntax:_.isSet(value)Parameters: This method accepts a single parameter as mentioned above and described below: value: This parameter holds the value to check.Return Value: T

2 min read

Difference between Regular functions and Arrow functions

This article discusses the major differences between regular functions and arrow functions. Arrow functions - a new feature introduced in ES6 - enable writing concise functions in JavaScript. While both regular and arrow functions work in a similar manner, there are certain interesting differences between them, as discussed below. Syntax: Regular f

2 min read

Difference Between deque::assign and deque::empty in C++

Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc

2 min read

What are User-defined Functions and Built-in Functions in PHP?

In PHP, User-defined functions are created by programmers to meet specific requirements, while PHP built-in functions are provided by PHP to perform common tasks without the need for manual implementation. Both types of functions play crucial roles in PHP development, offering flexibility, modularity, and efficiency in coding. Table of Content User

2 min read

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

Difference between isset() and empty() Functions - GeeksforGeeks (2)

'); $('.spinner-loading-overlay').show(); jQuery.ajax({ url: writeApiUrl + 'create-improvement-post/?v=1', type: "POST", contentType: 'application/json; charset=utf-8', dataType: 'json', xhrFields: { withCredentials: true }, data: JSON.stringify({ gfg_id: post_id, check: true }), success:function(result) { jQuery.ajax({ url: writeApiUrl + 'suggestions/auth/' + `${post_id}/`, type: "GET", dataType: 'json', xhrFields: { withCredentials: true }, success: function (result) { $('.spinner-loading-overlay:eq(0)').remove(); var commentArray = result; if(commentArray === null || commentArray.length === 0) { // when no reason is availaible then user will redirected directly make the improvment. // call to api create-improvement-post $('body').append('

'); $('.spinner-loading-overlay').show(); jQuery.ajax({ url: writeApiUrl + 'create-improvement-post/?v=1', type: "POST", contentType: 'application/json; charset=utf-8', dataType: 'json', xhrFields: { withCredentials: true }, data: JSON.stringify({ gfg_id: post_id, }), success:function(result) { $('.spinner-loading-overlay:eq(0)').remove(); $('.improve-modal--overlay').hide(); $('.unlocked-status--improve-modal-content').css("display","none"); $('.create-improvement-redirection-to-write').attr('href',writeUrl + 'improve-post/' + `${result.id}` + '/', '_blank'); $('.create-improvement-redirection-to-write')[0].click(); }, error:function(e) { $('.spinner-loading-overlay:eq(0)').remove(); var result = e.responseJSON; if(result.detail.non_field_errors.length){ $('.improve-modal--improve-content .improve-modal--improve-content-modified').text(`${result.detail.non_field_errors}.`); jQuery('.improve-modal--overlay').show(); jQuery('.improve-modal--improvement').show(); $('.locked-status--impove-modal').css("display","block"); $('.unlocked-status--improve-modal-content').css("display","none"); $('.improve-modal--improvement').attr("status","locked"); $('.improvement-reason-modal').hide(); } }, }); return; } var improvement_reason_html = ""; for(var comment of commentArray) { // loop creating improvement reason list markup var comment_id = comment['id']; var comment_text = comment['suggestion']; improvement_reason_html += `

${comment_text}

`; } $('.improvement-reasons_wrapper').html(improvement_reason_html); $('.improvement-bottom-btn').html("Create Improvement"); $('.improve-modal--improvement').hide(); $('.improvement-reason-modal').show(); }, error: function(e){ $('.spinner-loading-overlay:eq(0)').remove(); // stop loader when ajax failed; }, }); }, error:function(e) { $('.spinner-loading-overlay:eq(0)').remove(); var result = e.responseJSON; if(result.detail.non_field_errors.length){ $('.improve-modal--improve-content .improve-modal--improve-content-modified').text(`${result.detail.non_field_errors}.`); jQuery('.improve-modal--overlay').show(); jQuery('.improve-modal--improvement').show(); $('.locked-status--impove-modal').css("display","block"); $('.unlocked-status--improve-modal-content').css("display","none"); $('.improve-modal--improvement').attr("status","locked"); $('.improvement-reason-modal').hide(); } }, }); }); $('.left-arrow-icon_wrapper').on('click',function(){ if($('.improve-modal--suggestion').is(":visible")) $('.improve-modal--suggestion').hide(); else{ $('.improvement-reason-modal').hide(); } $('.improve-modal--improvement').show(); }); jQuery('.suggest-bottom-btn').on('click', function(){ var suggest_val = $.trim($("#suggestion-section-textarea").val()); var error_msg = false; if(suggest_val != ""){ if(suggest_val.length <= 2000){ jQuery('body').append('

'); jQuery('.spinner-loading-overlay').show(); jQuery.ajax({ type:'post', url: "https://apiwrite.geeksforgeeks.org/suggestions/auth/create/", xhrFields: { withCredentials: true }, crossDomain: true, contentType:'application/json', data: JSON.stringify({ "gfg_post_id" : `${post_id}`, "suggestion" : `

${suggest_val}

` }), success:function(data) { jQuery('.spinner-loading-overlay:eq(0)').remove(); jQuery('#suggestion-section-textarea').val(""); jQuery('.suggest-bottom-btn').html("Sent "); setTimeout(() => { jQuery('.improve-modal--overlay').hide(); $('.improve-modal--suggestion').hide(); }, 1000); }, error:function(data) { jQuery('.spinner-loading-overlay:eq(0)').remove(); jQuery('#suggestion-modal-alert').html("Something went wrong."); jQuery('#suggestion-modal-alert').show(); error_msg = true; } }); } else{ jQuery('#suggestion-modal-alert').html("Character limit exceeded."); jQuery('#suggestion-modal-alert').show(); jQuery('#suggestion-section-textarea').focus(); error_msg = true; } } else{ jQuery('#suggestion-modal-alert').html("Enter valid input."); jQuery('#suggestion-modal-alert').show(); jQuery('#suggestion-section-textarea').focus(); error_msg = true; } if(error_msg){ setTimeout(() => { jQuery('#suggestion-section-textarea').focus(); jQuery('#suggestion-modal-alert').hide(); }, 3000); } }) $('.improvement-bottom-btn.create-improvement-btn').click(function() { //create improvement button is clicked $('body').append('

'); $('.spinner-loading-overlay').show(); // send this option via create-improvement-post api jQuery.ajax({ url: writeApiUrl + 'create-improvement-post/?v=1', type: "POST", contentType: 'application/json; charset=utf-8', dataType: 'json', xhrFields: { withCredentials: true }, data: JSON.stringify({ gfg_id: post_id }), success:function(result) { $('.spinner-loading-overlay:eq(0)').remove(); $('.improve-modal--overlay').hide(); $('.improvement-reason-modal').hide(); $('.create-improvement-redirection-to-write').attr('href',writeUrl + 'improve-post/' + `${result.id}` + '/', '_blank'); $('.create-improvement-redirection-to-write')[0].click(); }, error:function(e) { $('.spinner-loading-overlay:eq(0)').remove(); var result = e.responseJSON; if(result.detail.non_field_errors.length){ $('.improve-modal--improve-content .improve-modal--improve-content-modified').text(`${result.detail.non_field_errors}.`); jQuery('.improve-modal--overlay').show(); jQuery('.improve-modal--improvement').show(); $('.locked-status--impove-modal').css("display","block"); $('.unlocked-status--improve-modal-content').css("display","none"); $('.improve-modal--improvement').attr("status","locked"); $('.improvement-reason-modal').hide(); } }, }); });

Difference between isset() and empty() Functions - GeeksforGeeks (2024)

FAQs

Difference between isset() and empty() Functions - GeeksforGeeks? ›

The isset() and ! empty() functions are similar and both will return the same results. But the only difference is ! empty() function will not generate any warning or e-notice when the variable does not exists.

What is the difference between Isset and empty in Wordpress? ›

isset() : You can use isset() to determine if a variable is declared and is different than null . empty() : It is used to determine if the variable exists and the variable's value does not evaluate to false .

What is the use of the isset() function? ›

The isset function in PHP is used to determine whether a variable is set or not. A variable is considered as a set variable if it has a value other than NULL. In other words, you can also say that the isset function is used to determine whether you have used a variable in your code or not before.

What's the difference between isset() and array_key_exists()? ›

The main difference when working on arrays is that array_key_exists returns true when the value is null , while isset will return false when the array value is set to null . See the PHP documentation for isset() . isset returns false and not null. Corrected, though of course deceze has the more complete answer by now.

Is Isset () is used to determine if a variable is set and is not NULL True False? ›

The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.

Is there a difference between isset and empty? ›

The isset() function will generate a warning or e-notice when the variable does not exists. The empty() function will not generate any warning or e-notice when the variable does not exists.

How to use an empty function in PHP? ›

PHP empty() Function

The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.

How to check if it's not empty in PHP? ›

We can use empty() function to check whether a string is empty or not. The function is used to check whether the string is empty or not.

What is the replacement of Isset in PHP? ›

Code Inspection: 'isset' can be replaced with coalesce

null coalesce operator. See Null coalesce operator (php.net) for details.

How to check if an object is empty or not in PHP? ›

Using empty() won't work as usual when using it on an object, because the __isset() overloading method will be called instead, if declared. Therefore you can use count() (if the object is Countable). Or by using get_object_vars() , e.g.

What is the opposite of Isset ()? ›

isset(); is a function that returns boolean true if a variable is not null. The opposite of isset(); is is_null(); In other words, it returns true only when the variable is null. The only difference is that isset() can be applied to unknown variables, but is_null() only to declared variables.

How to check if an array is empty in PHP? ›

Using the count() function

Another way of checking if a PHP array is empty is to use the count function. This function returns the number of items in the array as an integer which we can then check against.

How to check if key exists in PHP? ›

PHP array_key_exists() Function

The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.

What does isset() function? ›

isset() Function: The isset() function checks whether a variable is set, which means that it has tobe declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false Example: <html> <body> <?

How do you validate if a variable is null? ›

The easiest way to check if a value is either undefined or null is by using the equality operator ( == ). The equality operator performs type coercion, which means it converts the operands to the same type before making the comparison.

Why do we use $_request in PHP? ›

When a user clicks the submit button, the form data is sent to a PHP file specified in the action attribute of the <form> tag. In the action file we can use the $_REQUEST variable to collect the value of the input field.

How do I know if my post content is empty in WordPress? ›

So now if you want to check if the WordPress content is really empty, you can do this: if (empty_content($post->post_content)) { ... } This will return true if the content is empty; false if it's not.

What is the difference between Isset and unset? ›

If a variable has been unset with the unset() function, it is no longer considered to be set. isset() will return false when checking a variable that has been assigned to null . Also note that a null character ( "\0" ) is not equivalent to the PHP null constant.

What are the two content types that come installed with WordPress? ›

Default vs Custom Post Types
Default Post Types
AvailabilityAvailable by default when you install WordPress.
TypesPosts, Pages, and Media.
FlexibilityLimited to the built-in options.
UsageIdeal for standard blog posts, static pages, and media files.
Mar 20, 2024

What is the difference between empty and null text? ›

An empty string is useful when the data comes from multiple resources. NULL is used when some fields are optional, and the data is unknown.

Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated:

Views: 5913

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.