phpshorthand-if

PHP - Short-Hand If Syntax Error


so I originally have this code, but it gives a Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING:

.($page > 1 ? "<li><a href='browse.php?p=".$page-1."'></a></li>" : "")

I checked again and again for: '"'", but I got none.

I changed .$page-1. to .($page-1)., but still nothing

Then it worked by adding a new variable:

$pagem1 = $page-1;
...
.($page > 1 ? "<li><a href='browse.php?p=$pagem1'></a></li>" : "")

I solved my problem but I wonder, how can you achieve a 1-lined code without adding a new variable?

Full Code:

echo "<div class='pagination' style='font-size:18px; display:block;'>
        <span>Page $page of $totalpages</span>
        <ul class='pull-right'>
            ".($page > 2 ? "<li><a href=''>First</a></li>" : "")
            .($page > 2 ? "<li class='page-dots'><span>...</span></li>" : "")
            .($page > 1 ? "<li><a href='browse.php?p=".$page-1."'><i class='fa fa-long-arrow-left'></i></a></li>" : "") //First error on this line
            .($page == 1 ? "<li><a class='disabled'>1</a></li>" : ($page != $totalpages ? "<li><a href='browse.php?p=".$page-1."'>".$page-1."</a></li>" : "<li><a href='browse.php?p=".$page-2."'>".$page-2."</a></li>"))
            .($page != 1 && $page != $totalpages ? "<li><a class='disabled'>$page</a></li>" : ($page == 1 ? "<li><a href='browse.php?p=".$page+1."'>".$page+1."</a></li>" : "<li><a href='browse.php?p=".$page-1."'>".$page-1."</a></li>"))
            .($page == $totalpages ? "<li><a class='disabled'>1</a></li>" : ($page != 1 ? "<li><a href='browse.php?p=".$page+1."'>".$page+1."</a></li>" : "<li><a href='browse.php?p=".$page+2."'>".$page+2."</a></li>"))
            .($page < $totalpages - 2 ? "<li class='page-dots'><span>...</span></li>" : "")
            .($page < $totalpages - 2 ? "<li><a href='browse.php?p=".$totalpages."'>Last</a></li>" : "")
            .($page < $totalpages - 1 ? "<li><a href='browse.php?p=".$page+1."'><i class='fa fa-long-arrow-right'></i></a></li>" : "")."
        </ul>
    </div>
";

This code is reconstructed, but this original one had no quotes problems


Solution

  • Parentheses? ".($page-1)."

    Otherwise, you have