Failed to compare empty string

my code is:

$disabled = $this->access->get_access('disable_header'); $emptyString = ''; var_dump($emptyString); var_dump($disabled[0]); if($disabled[0] == '') die('should be here'); if($disabled[0] == ' ') die('should be here'); die('stop'); 

and the result:

 string(0) "" string(1) "" stop 

all my condition is not working and I don’t know why ..
but if I make the condition $ emptyString:

 if($emptyString == '') die('should be here'); 

this gives me the result:

 should be here 

if you see that both $ dislabled [0] and $ emptyString have the same empty string, but have different lengths, if I make $ emptyString up to 1, then:

 $disabled = $this->access->get_access('disable_header'); $emptyString = ' '; var_dump($emptyString); var_dump($disabled[0]); if($disabled[0] == '') die('should be here'); if($disabled[0] == ' ') die('should be here'); die('stop'); 

become:

 string(1) " " string(1) "" stop 

I could not compare $ disable [0]

what did I miss?

==== HOW TO SOLVE ===

I try first

 mb_detect_encoding($disabled[0]); 

then give me the result

 ASCII 

then I try:

 var_dump(hexdec($disabled[0])) 

then give me the result:

 int(9) 

i go to ascii table and press 9 = TAB

then now I make a condition:

 if(strcmp($disabled[0],'')) die('should be here'); 

tadaaa .. he shows:

 should be here 

I think strcmp can work for all input values, tabs and spaces. Any mistake for my opinion?

+6
source share
1 answer

I think,

 if(empty($disabled[0])) die('should be here'); 

is the best way to check if a variable is empty using PHP.

"" or " " for JavaScript.

+1
source

All Articles