2009-07-14 87 views
1

這是什麼問題?如果$ forwardformat不爲空,則運行「if語句」中的代碼,但如果$ forwardformat爲空,則「else」代碼不會運行。有任何想法嗎?!if(!empty)issues

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"; 
    } 
} 
+0

什麼是da ta類型的$ row ['id']? – Mohamed 2009-07-14 08:32:55

回答

3
  1. 如果$ forwardformat不爲空,它確實應該進入if語句。

  2. 關於else語句,只有在它爲空時纔會出現。所以你可能不得不考慮$ forwardformat不是空的。白色空間可能?嘗試在邊界之間回顯$前進以確保這一點。

+4

使用var_dump代替echo – Flo 2009-07-14 08:32:10

0

試試這個調查的$ 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"; 
     } 
    } 
6

看到的是空的認爲是空

返回FALSE的事情列表中,如果VAR有一個非空和非零內容值。

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) 
+0

好的答案 - 好的細節。 – Fenton 2010-10-15 13:49:44

2

我覺得,你的問題是這一行:


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

這隻空字符匹配。製表符,換行符等不會被替換(並不能真正顯示在您的(HTML的)輸出呼應的結果。因此,我建議,當你嘗試


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

HTH

Argelbargel

0

如果你有你的分貝多字節字符串數據(如與普通的UTF-8編碼)...

檢查出mb_strlen()...比較它與strlen(),如果它真的應該返回0空。