How to check for empty string in PHP ? - GeeksforGeeks (2024)

Skip to content

How to check for empty string in PHP ? - GeeksforGeeks (1)

Last Updated : 04 Jun, 2024

Improve

In this article, we will see how to check for empty string in PHP. String is a set of characters. A string is said to be empty, if it contains no characters. 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. It will return true if the string is empty.

Syntax:

bool empty(string)

Parameter: Variable to check whether it is empty or not.

Return Value: If string is empty, it returns true and false otherwise.

Example 1: PHP program to check whether the string is empty or not.

PHP
<?php// Consider a string which is empty$s = "";// Return a message if string is emptyif(empty($s)) { echo "Empty string";}else { echo "Not empty";}?>

Output

Empty string

Example 2:

PHP
<?php// Consider a string which is not empty$s = "Welcome to GFG";// Return a message if string is emptyif(empty($s)) { echo "Empty string";}else { echo "Not empty";}?>

Output

Not empty

Using the strlen() function

Using the strlen() function, you can check if a string is empty by evaluating its length. If `strlen($string) === 0`, the string is empty. This approach ensures precise length measurement, distinguishing between empty strings and other empty values like `NULL` or `0`.

Example:

PHP
<?php// Consider a string which is empty$s = "";// Return a message if string is emptyif (strlen($s) === 0) { echo "Empty string";} else { echo "Not empty";}?>

Output

Empty string

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

How to check whether an array is empty using PHP?

In this article, we are going to learn how can we check whether an array is empty using PHP. There are various methods and functions available in PHP to check whether the defined or given array is empty or not. These are the following methods by using these we can solve our problem: Table of Content Using empty() Function:Using count FunctionUsing

2 min read

How to Check Whether a Variable is Empty in PHP?

Given some values of variables, the task is to check whether the variable is empty or not in PHP. An empty variable can refer to a variable that has not been set, a variable that has been explicitly set to an empty value, or a variable that contains a value that is considered empty. In this article, we will explore different approaches to check whe

3 min read

How to check empty/undefined/null string in JavaScript?

Empty strings contain no characters, while null strings have no value assigned. Checking for an empty, undefined, or null string in JavaScript involves verifying if the string is falsy or has a length of zero. To check for empty, undefined, or null strings in JavaScript, use conditional statements, string comparison methods, or the typeof operator

2 min read

PHP | empty() Function

The empty() function is an inbuilt function in PHP which is used to check whether a variable is empty or not. Syntax: bool empty ( $var ) Parameter: This function accepts a single parameter as shown in above syntax and described below. $var: Variable to check whether it is empty or not. Note: Below version of PHP 5.5, empty() only supports variable

2 min read

Program to remove empty array elements in PHP

Given an array containing elements. The task is to remove empty elements from the array such as an empty string or a NULL element. Method 1: Using array_filter() Function. It is achieved by using array_filter() function. It also removes false values when declared using a callback function, however, if no callback function is specified, all the valu

2 min read

Best way to initialize empty array in PHP

Arrays in PHP: Use array() Function to create an array in PHP. There are three types of array supported in PHP: Indexed arrays: Arrays having a numeric index. Associative arrays: Arrays having named keys. Multidimensional arrays: It contains one or more array in particular array. Note: Why it is always good practice to declare an empty array and th

3 min read

Why would $_FILES be empty when uploading files to PHP ?

While uploading the files or writing the code, developers do many mistakes which we cannot figure out most of the time. Some spelling mistakes, some forgotten parts in the code lead to failed uploading of the file and give warnings or errors. To avoid this kind of mistake, we should learn about the common mistakes occurring for a better understandi

3 min read

How to Define an Empty Object in PHP ?

An object is an instance of a class, and we can create objects using various approaches. Defining an empty object typically involves creating an instance without specifying a class definition explicitly. In this article, we will explore different approaches to defining an empty object in PHP, these are: Table of Content Using stdClassUsing Casting

1 min read

How to check an HTML element is empty using jQuery ?

Given an HTML document and select an element from the HTML document and check that element is empty or not. There are two methods used to solve this problem which are discussed below: Method 1: Using the ":empty" selector: The element to be checked using is() method. The is() method is used to check if one of the selected elements matches to the se

3 min read

How to check for empty string in PHP ? - GeeksforGeeks (2)

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

How to check for empty string in PHP ? - GeeksforGeeks (3)

'); $('.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(); } }, }); });

How to check for empty string in PHP ? - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Dr. Pierre Goyette

Last Updated:

Views: 5963

Rating: 5 / 5 (70 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Dr. Pierre Goyette

Birthday: 1998-01-29

Address: Apt. 611 3357 Yong Plain, West Audra, IL 70053

Phone: +5819954278378

Job: Construction Director

Hobby: Embroidery, Creative writing, Shopping, Driving, Stand-up comedy, Coffee roasting, Scrapbooking

Introduction: My name is Dr. Pierre Goyette, I am a enchanting, powerful, jolly, rich, graceful, colorful, zany person who loves writing and wants to share my knowledge and understanding with you.