2011-09-19 69 views
0

我很新的php。我在檢查狀態時遇到問題。我有這樣問題在條件鏈接

if(condition1){ 

    if (condition2){ 
     Statement1; 
    }else{ 
     Statement2; 
    } 
else{ 

    if (same condition2){ 
     Statement3; 
    }else{ 
     Statement4; 
} 

條件鏈現在,當我寫這就像我有錯誤 -

if (condition1) "AND". (condition2) 

    Statement1; 
if (condition1) "AND". (!condition2) 

    Statement2; 
if (!condition1) "AND". (condition2) 

    Statement3; 
if (!condition1) "AND". (!condition2) 

    Statement4; 

誰能賜教我在哪裏錯了?

這裏是我寫的實際代碼 -

if (!isset($store['stationfilter'])) "AND". ($fromdate_display == $todate_display) 

$storeSql_current = sprintf($sql_current, ''); //error 1 

if (!isset($store['stationfilter'])) "AND". (!$fromdate_display == $todate_display)  //error2 

$storeSql_otherThancurrent = sprintf($sql_otherThancurrent, ''); //error3 

if (isset($store['stationfilter'])) "AND". ($fromdate_display == $todate_display) //error4 

$storeSql_current = sprintf($sql_current, "AND" . $store['stationfilter']); //error5 

if (isset($store['stationfilter'])) "AND". (!$fromdate_display == $todate_display) //error6 

$storeSql_otherThancurrent = sprintf($sql_otherThancurrent, "AND" . $store['stationfilter']); //error7 

請告訴我,我錯了。

感謝, 尼爾

+2

你覺得'(condition1)「AND」。 (條件2)'應該做的?在一個變量'condition1'之後加上''AND''有什麼意義? PHP可能會拋出一個語法錯誤,這應該給你一個關於錯誤的提示。尤其是當你是新手時,你應該閱讀[documentation](http://php.net/manual/en/control-structures.if.php),這應該可以幫助你解決大部分問題。 –

+0

你的第二個代碼沒有意義。這些真實條件是什麼?有可能是最簡單的方式來實現相同的 –

回答

4

你應該這樣寫:

if ($condition1 && $condition2) .... 
+0

它不工作 - 拋出語法錯誤,意想不到的T_BOOLEAN_AND在/home/foldername/public_html/admin/program.php在線152 – Neel

+0

我很抱歉它不工作,但只是告訴你如何在if語句中寫入'和'。 – xdazz

2
if ($cond1 && $cond2) { 

     # stmt1 

    } else if ($cond1) { 

     # stmt2 

    } else if ($cond2) { 

     # stmt3 

    } else { 

     # stmt4 

    } 

我已經修改了上面的實際情況,在下面。我也採取了猜測你的語句應該看起來像......

if (isset($store['stationfilter']) && $fromdate_display === $todate_display) { 

    $storeSql_current = sprintf('%s AND %s', $sql_current, $store['stationfilter']); 

} else if (isset($store['stationfilter'])) { 

    $storeSql_otherThancurrent = sprintf('%s AND %s', $sql_otherThancurrent, $store['stationfilter']); 

} else if ($fromdate_display === $todate_display) { 

    $storeSql_current = (string) $sql_current; 

} else { 

    $storeSql_otherThancurrent = (string) $sql_otherThancurrent; 

} 

如果你正在做的是建立一個SQL語句,那麼這種方法可能因爲潛在的危險SQL注入。如果$store['stationfilter'],$sql_current$sql_otherThancurrent(數據)來自不可信來源(除非來源是您寫的代碼,否則所有來源都不可信),那麼您至少應該使用數據庫適配器來引用數據,或者更好地,使用參數化查詢。