简体   繁体   中英

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

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 . PHP doesn't care what that HTML is, as far as it's concerned it's just a string. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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