2013-05-03 77 views
1

回聲信息我有一個表格審覈頁面的偉大工程,電話:PHP if語句從形式

echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>"; 
echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Email:</span>{$_REQUEST['CustomerEmail']}</div>"; 
echo "<div class='reviewItem'><span class='reviewTitle'>Customer Phone:</span>{$_REQUEST['CustomerPhone']}</div>"; 
echo "<div class='reviewItem'><span class='reviewTitle'>Customer Address:</span>{$_REQUEST['CustomerAddress']}</div>"; 

的問題是,我有一噸沒有得到填補了領域。我想在echo之前加上一個「if」語句來檢查它是否有數據。如果不是,則不顯示任何內容。我對PHP很陌生,所以我不確定是否可以打電話給我:

if (length > 0)echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Email:</span>{$_REQUEST['CustomerEmail']}</div>"; 

在此先感謝您的幫助。乾杯。

+0

echo(condition)? 「

」:false; – 2013-05-03 18:27:31

回答

1
echo (!empty($_REQUEST['CustomerName'])) ? "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span> {$_REQUEST['CustomerName']}</div>" : ""; 
echo (!empty($_REQUEST['CustomerEmail'])) ? "<div class='reviewItem'><span class='reviewTitle'>Cusomer Email:</span>{$_REQUEST['CustomerEmail']}</div>" : ""; 
echo (!empty($_REQUEST['CustomerPhone'])) ? "<div class='reviewItem'><span class='reviewTitle'>Customer Phone:</span>{$_REQUEST['CustomerPhone']}</div>" :""; 
echo (!empty($_REQUEST['CustomerAddress'])) ? "<div class='reviewItem'><span class='reviewTitle'>Customer Address:</span>{$_REQUEST['CustomerAddress']}</div>" : ""; 

嘗試。

+0

謝謝,這工作完美! – Packy 2013-05-03 19:27:28

0

嘗試

if (isset($_REQUEST['CustomerName'])) 
    echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>"; 

if (isset($_REQUEST['CustomerName']) && $_REQUEST['CustomerName']) 
    echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>"; 
0

你只是得到它。試試這個:

if(isset($_REQUEST['CustomerName']})) 
    echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>"; 

PHP的isset檢查,看是否被引用變量賦值 - 如果這樣做,回聲它。

0

我通常會這樣寫,測試的VAR被設置,而不是空白:

<?php 
if(isset($_REQUEST['CustomerName']) && $_REQUEST['CustomerName']!=''){ 
echo "<div class='reviewItem'><span class='reviewTitle'>Customer Name:</span>{$_REQUEST['CustomerName']}</div>"; 
} 
?> 
+1

如果你想阻止空字符串,你不需要檢查兩個條件:'if(!empty($ _ REQUEST ['CustomerName')){...}' – Madbreaks 2013-05-03 18:36:22

+0

好點。我忘了你可以用空。 – rncrtr 2013-05-03 19:17:08

0

你說這個?

if($_REQUEST['CustomerName']): 
    echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>"; 
endif; 
if($_REQUEST['CustomerEmail']): 
    echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Email:</span>{$_REQUEST['CustomerEmail']}</div>"; 
endif; 
if($_REQUEST['CustomerPhone']): 
    echo "<div class='reviewItem'><span class='reviewTitle'>Customer Phone:</span>{$_REQUEST['CustomerPhone']}</div>"; 
endif; 
if($_REQUEST['CustomerAddress']): 
    echo "<div class='reviewItem'><span class='reviewTitle'>Customer Address:</span>{$_REQUEST['CustomerAddress']}</div>"; 
endif; 
+2

我不明白爲什麼人們仍然使用'if:endif;',特別是在這種情況下。它只是噪音 – Madbreaks 2013-05-03 18:29:48

+0

它更容易閱讀,如果你有其他的東西,如HTML等..你會想知道什麼是'}'意味着流浪漢,對吧?除非你想追溯到上面.. – reikyoushin 2013-05-03 18:30:58

+0

你調用'}'=>流浪? – 2013-05-03 18:32:57

0

您可以隨時使用短的路:

echo (isset($_REQUEST['CustomerName'])) ? "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>" : false; 

邏輯:

echo (condition) ? TRUE : FALSE; 
+0

它會工作嗎?是。但是,有些關於'echo false'的東西讓我感覺錯了。那這個「短路」又是如何?這比使用簡單的if語句更多的字符。 – Madbreaks 2013-05-03 18:39:38