How to check whether a variable is empty in php

Closed.

Bạn đang xem: How to check whether a variable is empty in php

This question is not reproducible or was caused by typos. It is not currently accepting answers.
Want to lớn improve this question? Update the question so it's on-topic for Stack Overflow.

Closed last year.


I have a function isNotEmpty which returns true if the string is not empty and false if the string is empty. I"ve found out that it is not working if I pass an empty string through it.

function isNotEmpty($input) $strTemp = $input; $strTemp = trim($strTemp); if(strTemp != "") //Also tried this "if(strlen($strTemp) > 0)" return true; return false;The validation of the string using isNotEmpty is done:

if(isNotEmpty($userinput<"phoneNumber">)) //validate the phone numberelse emang lại "Phone number not entered";If the string is empty the else doesn"t exeđáng yêu, I don"t understvà why, can someone please shed some light on this please.


*

*

Simple problem actually. Change:

if (strTemp != "")lớn

if ($strTemp != "")Arguably you may also want khổng lồ change it to:

if ($strTemp !== "")since != "" will return true if you pass is numeric 0 và a few other cases due khổng lồ PHP"s automatic type conversion.

You should not use the built-in empty() function for this; see comments & the PHPhường type comparison tables.


*

*

I always use a regular expression for checking for an empty string, dating bachồng lớn CGI/Perl days, và also with Javascript, so why not with PHP. as well, e.g. (albeit untested)

return preg_match("/S/", $input);Where S represents any non-whitespace character


*

PHP.. have a built in function called empty()the test is done by typing if(empty($string))...Reference php.net : php empty


In your if clause in the function, you"re referring to lớn a variable strTemp that doesn"t exist. $strTemp does exist, though.

Xem thêm: Nghĩa Của Từ Trade Show Là Gì, Định Nghĩa Và Giải Thích Ý Nghĩa

But PHPhường. already has an empty() function available; why make your own?

if (empty($str)) /* String is empty */else /* Not empty */From php.net:

Return Values

Returns FALSE if var has a non-empty & non-zero value.

The following things are considered khổng lồ be empty:

* "" (an empty string)* 0 (0 as an integer)* "0" (0 as a string)* NULL* FALSE* array() (an empty array)* var $var; (a variable declared, but without a value in a class)http://www.php.net/empty


PHP evaluates an empty string khổng lồ false, so you can simply use:

if (trim($userinput<"phoneNumber">)) // validate the phone number else eđến "Phone number not entered";
I just write my own function, is_string for type checking & strlen khổng lồ kiểm tra the length.

function emptyStr($str) return is_string($str) &và strlen($str) === 0;print emptyStr("") ? "empty" : "not empty";// emptyHere"s a small thử nghiệm repl.it

EDIT: You can also use the tryên ổn function to demo if the string is also blank.

is_string($str) &và strlen(trim($str)) === 0;
I needed khổng lồ demo for an empty field in PHP & used


Well here is the short method to lớn kiểm tra whether the string is empty or not.

$input; //Assuming lớn be the stringif(strlen($input)==0)return false;//if the string is emptyelsereturn true; //if the string is not empty
You can simply cast khổng lồ bool, dont forget to handle zero.

Xem thêm: Những Hình Ảnh Hài Hước Có Một Không Hai Trong Cuộc Sống, Loạt Ảnh Có Một Không Hai Nhờ Chụp Đúng Thời Điểm

function isEmpty(string $string): bool if($string === "0") return false; return !(bool)$string;var_dump(isEmpty("")); // bool(true)var_dump(isEmpty("foo")); // bool(false)var_dump(isEmpty("0")); // bool(false)
I know this thread been pretty old but I just wanted lớn mô tả one of my function. This function below can kiểm tra for empty strings, string with maximum lengths, minimum lengths, or exact length. If you want to lớn kiểm tra for empty strings, just put $min_len và $max_len as 0.

function chk_str( $input đầu vào, $min_len = null, $max_len = null ) if ( !is_int($min_len) && $min_len !== null ) throw new Exception("chk_str(): $min_len must be an integer or a null value."); if ( !is_int($max_len) &và $max_len !== null ) throw new Exception("chk_str(): $max_len must be an integer or a null value."); if ( $min_len !== null && $max_len !== null ) if ( $min_len > $max_len ) throw new Exception("chk_str(): $min_len can"t be larger than $max_len."); if ( !is_string( $input đầu vào ) ) return false; else $output = true; if ( $min_len !== null ) if ( strlen($input) $max_len ) $output = false; return $output;