简体   繁体   English

如果条件在href标签中怎么写?

[英]How can i write this if condition inside the href tag?

I don't know how to write the if-condition inside the Href .The href is written in the PHP tags . 我不知道怎么写的if-condition里面Href .The href是写在PHP tags and it gives me an error that Parse error: syntax error, unexpected 'if' (T_IF) in D:\\Xampp\\htdocs\\Portal\\admin\\pages\\ajax\\EditDeleteAssignment.php on line 84 它给我一个Parse error: syntax error, unexpected 'if' (T_IF) in D:\\Xampp\\htdocs\\Portal\\admin\\pages\\ajax\\EditDeleteAssignment.php on line 84

This is my code... 这是我的代码...

<?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) .

You're not necessarily writing this in an href , you're writing it in a string . 您不一定要href编写它,而要在字符串中编写它。 PHP doesn't care what that HTML is, as far as it's concerned it's just a string. PHP并不在乎HTML是什么,就它而言,它只是一个字符串。 Simplified to: 简化为:

$data .= '...' . if($pageno <= 1){ echo "#"; } else { echo "?pageno=".($pageno - 1); }. '...';

The syntax problem here is that you have multiple code lines, even multiple code blocks, on a single line of code. 这里的语法问题是,在一行代码上有多个代码行,甚至多个代码块。 Fortunately, for simple conditional statemens which result in just a result to output, there's the ternary conditional operator. 幸运的是,对于仅导致结果输出的简单条件语句,就有三元条件运算符。 Something like this: 像这样:

$data .= '...' . ($pageno <= 1 ? "#" : ("?pageno=" . ($pageno - 1))) . '...';

Notice the components thereing: 请注意其中的组件:

$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

Note also the various parentheses used. 还要注意使用的各种括号。 Combining multiple concatenation operators throughout this conditional expression could easily confuse the parser or the programmer. 在此条件表达式中组合多个串联运算符可能会很容易使解析器或程序员感到困惑。 Better to be explicit about the order of operations here. 最好在这里明确操作顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM