Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP best way to check whether a string is empty or not

Tags:

php

I've seen a lot of php code that does the following to check whether a string is valid by doing:

$str is a string variable.

if (!isset($str) || $str !== '') {   // do something } 

I prefer to just do

if (strlen($str) > 0) {   // something } 

Is there any thing that can go wrong with the second method? Are there any casting issues I should be aware of?

like image 802
Yada Avatar asked Dec 25 '10 19:12

Yada


People also ask

How do you check a string is empty or not 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 check string is empty or null in PHP?

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 . is_null() : This function is used to check if a variable is null .

How do I check if a string is empty or not?

The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.

How check 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.


2 Answers

Since PHP will treat a string containing a zero ('0') as empty, it makes the empty() function an unsuitable solution.

Instead, test that the variable is explicitly not equal to an empty string:

$stringvar !== ''

As the OP and Gras Double and others have shown, the variable should also be checked for initialization to avoid a warning or error (depending on settings):

isset($stringvar)

This results in the more acceptable:

if (isset($stringvar) && $stringvar !== '') { } 

PHP has a lot of bad conventions. I originally answered this (over 9 years ago) using the empty() function, as seen below. I've long since abandoned PHP, but since this answer attracts downvotes and comments every few years, I've updated it. Should the OP wish to change the accepted answer, please do so.

Original Answer:

if(empty($stringvar)) {     // do something } 

You could also add trim() to eliminate whitespace if that is to be considered.

Edit:

Note that for a string like '0', this will return true, while strlen() will not.

like image 76
JYelton Avatar answered Sep 22 '22 03:09

JYelton


You need isset() in case $str is possibly undefined:

if (isset($str) && $str !== '') {     // variable set, not empty string } 

Using !empty() would have an important caveat: the string '0' evaluates to false.


Also, sometimes one wants to check, in addition, that $str is not something falsy, like false or null[1]. The previous code doesn't handle this. It's one of the rare situations where loose comparison may be useful:

if (isset($str) && $str != '') {     // variable set, not empty string, not falsy } 

The above method is interesting as it remains concise and doesn't filter out '0'. But make sure to document your code if you use it.

Otherwise you can use this equivalent but more verbose version:

if (isset($str) && (string) $str !== '') {     // variable set, not empty string, not falsy } 


Of course, if you are sure $str is defined, you can omit the isset($str) from the above codes.


Finally, considering that '' == false, '0' == false, but '' != '0', you may have guessed it: PHP comparisons aren't transitive (fun graphs included).


[1] Note that isset() already filters out null.

like image 33
Gras Double Avatar answered Sep 22 '22 03:09

Gras Double