[英]How can i write this if condition inside the href tag?
我不知道怎么写的if-condition
里面Href
.The href
是写在PHP tags
。 它给我一个Parse error: syntax error, unexpected 'if' (T_IF) in D:\\Xampp\\htdocs\\Portal\\admin\\pages\\ajax\\EditDeleteAssignment.php on line 84
这是我的代码...
<?php $data .= '<tfooter>
<td colspan="2" class="text-center">
<ul class="pagination">
<li><a class="btn btn-primary" style="color:white;" href="?pageno=1" >First</a></li>
<li class="<?php if($pageno <= 1){ echo "disabled" } ">
<a class="btn btn-primary" style="color:white;" href="'.if($pageno <= 1){ echo "#"; } else { echo "?pageno=".($pageno - 1); }.'">Prev</a>
</table>';
echo $data;
?>
尝试使用三元If
($pageno <= 1) ? "#" : "?pageno=" . ($pageno - 1) .
您不一定要在href
编写它,而要在字符串中编写它。 PHP并不在乎HTML是什么,就它而言,它只是一个字符串。 简化为:
$data .= '...' . if($pageno <= 1){ echo "#"; } else { echo "?pageno=".($pageno - 1); }. '...';
这里的语法问题是,在一行代码上有多个代码行,甚至多个代码块。 幸运的是,对于仅导致结果输出的简单条件语句,就有三元条件运算符。 像这样:
$data .= '...' . ($pageno <= 1 ? "#" : ("?pageno=" . ($pageno - 1))) . '...';
请注意其中的组件:
$pageno <= 1 // <-- the condition being checked
? // <-- the start of the operator, what's before it is the condition
"#" // <-- the result if true
: // <-- the second part of the operator, basically the "else"
("?pageno=" . ($pageno - 1)) // <-- the result if false
还要注意使用的各种括号。 在此条件表达式中组合多个串联运算符可能会很容易使解析器或程序员感到困惑。 最好在这里明确操作顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.