tags:

views:

775

answers:

5

What is wrong with this? The code in the "if statement" runs if $forwardformat isn't empty empty, but the "else" code doesn't run if $forwardformat is empty. Any ideas?!

while ($row = mysql_fetch_array($forwardresult)) {
    $forward = $row["id"];
    $forwardformat = str_replace(" ","",$forward);

    if (!empty($forwardformat)) {
        echo 'Exploring moves us <a href="casestudy.php?id=';
        echo $forwardformat;
        echo '">forward</a>';
    }
    else {
        echo "forward";
    }
}
+3  A: 
  1. It should indeed enter the if statement if $forwardformat isn't empty.

  2. About the else statement, it only goes there if it is empty. So you might have to consider that $forwardformat is not empty. White spaces maybe ? Try to echo $forward between boundaries to be sure of that.

Amadeus45
use var_dump instead of echo
Flo
A: 

Try this to investigate the contents of $forwardformat

  
    while ($row = mysql_fetch_array($forwardresult)) {  
        $forward = $row["id"];  
        $forwardformat = str_replace(" ","",$forward);  
        if (!empty($forwardformat)) {  
            echo 'Exploring moves us <a href="casestudy.php?id='. $forwardformat .'">forward</a>';  
            //DEBUG
            echo "<textarea>";
            var_dump($forwardformat);
            echo "</textarea>";
        }  
        else {  
            echo "forward";  
        }  
    }  
phalacee
+4  A: 

see the list of thing that empty consider as empty

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

The following things are considered to 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)
Haim Evgi
Nice answer - good detail.
Sohnee
+2  A: 

I think, your problem is this line:


$forwardformat = str_replace(" ","",$forward);

This only matches the space-character. Tab, Newline etc. are not replaced (and do not really show in your (html-)output when echoing the result. Thus i recommend, you try


$forwardformat = preg_replace('/\s+/','',$forward);

HTH

Argelbargel

Argelbargel
A: 

If you've got multi-byte string data (like with common UTF-8 encoding) in your db...

Check out mb_strlen()... compare it with strlen(), which should return 0 if its truly empty.

Josh