如果$ var是空的不行吗?

I run this code:

<?

$map = do_shortcode('[codepeople-post-map]');    
if (empty($map)) {echo '<br />'; } else {echo $map;}

?>

The problem is when the $map is empty it won't echo the <br />

I have tried this 2 methods to see if $map is really empty:

  • echo $map; // It results empty and noting is shown on page.
  • var_dump($map); // It results: string(937) " "

My question is what does string(937) " " means and how can I make my code work? I tried also:

<? if ($map == string(937) " ") {echo '<br />'; } else {echo $map;} ?>

But no success so far this last code is wrong and just gives error.

A variable is considered empty if it does not exist or if its value equals FALSE

Your variable is a string with a space, which evaluates to TRUE, so it is not empty.

Instead of using empty(), you might check if trim($map) === ''.

Well, it means that you have a string with a space in it. Probably the var was previously a string which was emptied wrong. One solution I could suggest you, depending what your application's purpose is.

 if(trim($map) == "" || $map == null)
 {
      echo "<br />";
 }
 else
 {
      echo $map;
 }

Because actually it only means that the string contains a whitespace (this explains why there is a space in " "). You should try to locate where you get the $map value and try to put some verifications there.

Alex