PHP:我可以在<li>标签中放入IF语句吗?

时间:2017-05-04 16:39:48

标签: php html html-lists

好的,所以我正在尝试为学校网站制作一份清单,我想制作一个有序的清单,然后是复选框和说明。这是我的代码,但我收到一条错误消息,上面写着“意外的IF语句”。

echo "<ol type='1'>
       <li>" . if ($r['check1'] == 1){
           echo "<input type='checkbox' name='check1' value='1' checked>submitted my JSCC
                  admissions application for the upcoming term to JSCC Admissions and Records
                  Services.
                  \n<br>";
       } else {
           echo "<input type='checkbox' name='check1' value='1'>submitted my JSCC admissions
                  application for the upcoming term to JSCC Admissions and Records
                  Services.
                  \n<br>";} . "</li>

       </ol>\n";

2 个答案:

答案 0 :(得分:1)

您需要将if-else语句与echo语句分开:

echo "<ol type='1'><li>";
if ($r['check1'] == 1){
   echo "<input type='checkbox' name='check1' value='1' checked>submitted my JSCC admissions application for the upcoming term to JSCC Admissions and Records Services.\n<br>";
} else {
   echo "<input type='checkbox' name='check1' value='1'>submitted my JSCC admissions application for the upcoming term to JSCC Admissions and Records Services.\n<br>";
} 
echo "</li></ol>\n";

然后它将正确执行。

答案 1 :(得分:0)

您的代码中存在语法错误。在这里你可以使用这样的三元运算符。

<?php

echo "<ol type='1'><li>" . 
( $r['check1'] == 1  

        ? "<input type='checkbox' name='check1' value='1' checked>submitted my JSCCadmissions application for the upcoming term to JSCC Admissions and RecordsServices.\n<br>"
        : "<input type='checkbox' name='check1' value='1'>submitted my JSCC admissions application for the upcoming term to JSCC Admissions and Records Services. \n<br>"
) 
. "</li></ol>\n";