[英]How to put IF Statement inside string?
如何將 IF 語句放在這樣的字符串中?
<?php echo
"<div class='portfolio-info'>
<form id='imgtitle' class='kart' action='" . base_url() . "gallery/delete' method='post'>
<input type = 'hidden' name='delete_file' value='$image' />
//I want to put IF here
<button type = 'submit' class='btn btn-danger'><i class='delete fa fa-trash fa-3x'></i></button>
//END here
</form>
</div>";
?>
嘗試這個。
<?php echo
"<div class='portfolio-info'>
<form id='imgtitle' class='kart' action='" . base_url() . "gallery/delete' method='post'>
<input type = 'hidden' name='delete_file' value='$image' />
";
if(expression) //put IF here
{
//put multiple statements if any
echo "<button type = 'submit' class='btn btn-danger'><i class='delete fa fa-trash fa-3x'></i></button> ";
}
echo "</form></div>";
?>
在此之后,請了解“如何使用 HTML 嵌入 PHP”
希望這對你有幫助。
您可以預先測試您的條件並將結果放入變量中。 如果條件失敗,只需將其等於空字符串即可。 這里使用三元運算符進行賦值。
<?php
$base_url = '/';
$image = "foo";
$condition = true;
$submit = $condition
? "<button type = 'submit' class='btn btn-danger'><i class='delete fa fa-trash fa-3x'></i></button>"
: '';
echo
"<div class='portfolio-info'>
<form id='imgtitle' class='kart' action='${base_url}gallery/delete' method='post'>
<input type = 'hidden' name='delete_file' value='$image' />
$submit
</form>
</div>";
或者,您可以連接字符串並使用三元運算符。 但這可能會變得混亂。
<?php
echo
"<div class='portfolio-info'>
<form id='imgtitle' class='kart' action='${base_url}gallery/delete' method='post'>
<input type = 'hidden' name='delete_file' value='$image' />
" . ($condition ? "<button type = 'submit' class='btn btn-danger'><i class='delete fa fa-trash fa-3x'></i></button>" : ''). "
</form>
</div>";
你可以去掉括號,使用帶有逗號的 echo :
<?php
echo
"<div class='portfolio-info'>
<form id='imgtitle' class='kart' action='${base_url}gallery/delete' method='post'>
<input type = 'hidden' name='delete_file' value='$image' />",
$condition ? "<button type = 'submit' class='btn btn-danger'><i class='delete fa fa-trash fa-3x'></i></button>" : '',
"</form>
</div>";
我解決了這個問題:
<?php
<div class='portfolio-info'>
<form id='imgtitle' class='kart' action='" . base_url() . "gallery/delete' method='post'>
<input type = 'hidden' name='delete_file' value='$image' />";?>
<?php if($_SESSION['admin']==1):?>
<button type = 'submit' class='btn btn-danger'><i class='delete fa fa-trash fa-3x'></i></button>
<?php endif; ?>
<?="</form>
</div>
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.