by Sai gowtham

How to check whether a string is empty in PHP

To check whether a given string is empty or not, we can use the built-in empty() function in PHP language.

The empty() function returns true if a string is empty; otherwise it returns false.

Here is an example:

$myString = "";

// True because $myString is empty
if (empty($myString)) {
  echo "String is empty.";
}else{
  echo "String is not empty.";
}

Output:

String is empty.

Similarly, we can also use the strlen() function to check for a empty string.

$myString = "";

if (strlen($myString < 0)) {
  echo "String is empty.";
}else{
  echo "String is not empty.";
}

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
130,921 students enrolled
48.5 hours of video content
View Course