2015-04-15 88 views
-1

我一直試圖在滿足特定條件時在<li>標記之間顯示內容。最初我有一個$結果字符串,我試圖檢查一個條件。但我想我的語法錯了。 我的代碼:無法在html中運行PHP腳本

$Result1 = 
'<div id="midrow"> 
<ul class="rounded-list"> 
<li ><strong>Name:</strong> '.$username.'</li> 
    htmlspecialchars('<? if($tips_flag == 1){ ?>'); 
    <li >Competency 6 - Offer concessions: '.round($per_s6, 2).' percent</li> 
    htmlspecialchars('<? } ?>)'; 
</ul> 
</div>'; 

我的這三條線是不正確的,我想,

echo htmlspecialchars('<? if($tips_flag == 1){'); 
<li >Competency 6 - Offer concessions: '.round($per_s6, 2).' percent</li> 
echo htmlspecialchars('<? } ?>)'; 

幫助PLZ!

+3

您無法在變量賦值中回顯。 – chris85

+0

刪除回聲,但仍然沒有成功 – nikhilk

+0

你想用'htmlspecialchars()'做什麼?你現在擁有的只是回顯純文本;它不會作爲PHP執行,因爲您將它作爲字符串參數傳遞。 –

回答

1

我認爲這是你之後(不知道有關htmlspecialchars雖然)。

$html_additional_content = ($tips_flag == 1) ? '<li>Competency 6 - Offer concessions: ' . round($per_s6, 2) . ' percent</li>' : ''; 
$Result1 = 
'<div id="midrow"> 
<ul class="rounded-list"> 
<li><strong>Name:</strong> '.$username.'</li>' . $html_additional_content .' 
</ul> 
</div>'; 

該第一行是使用三元運算符的條件。還要確保你的變量已經分配。

0

有些東西是錯誤的語法明智的。我不知道這是什麼代碼是做什麼,但語法聰明一點有什麼不對:

$Result1 = 
'<div id="midrow"> 
<ul class="rounded-list"> 
<li ><strong>Name:</strong> '.$username.'</li> //you need to put a '; here 
    echo htmlspecialchars('<? if($tips_flag == 1){'); //why do you have php 
    //opening there? 

    //here you need to do echo ' 
    <li >Competency 6 - Offer concessions: '.round($per_s6, 2).' percent</li> 
    //you need '; here to end the single tick with a semicolon 
    echo htmlspecialchars('<? } ?>)'; 

//you need echo ' here again 
</ul> 
</div>'; 
0

你不能打開,並在連接字符串中間關閉標籤(<?php?>)。您也不能將if區塊放入任一區域

$Result1 = 
'<div id="midrow"> 
<ul class="rounded-list"> 
<li ><strong>Name:</strong> ' . $username . '</li>'; 
if($tips_flag == 1) { 
    $Result1 .='<li >Competency 6 - Offer concessions: '.round($per_s6, 2).' percent</li>'; 
    } 
$Result1 .= '</ul> 
</div>';