如何在PHP echo中包含if语句?

时间:2016-04-06 10:42:29

标签: php html if-statement echo

 for ($i=0; $i<$total_questions_for_exam; $i++) {

        $question_id= $question_and_answers[$i]['question_id'];
        $numberofanswersperquestion = count_answers_belongToOne_questionNew($question_id);
        //die(var_dump($question_id)); 
        $student_answer_per_question = retrieve_student_result ($_SESSION['user_id'], $_GET['quiz_id'], $question_id);
         echo '   <table class="table table-bordered table-condensed table-datatable table-hover">
                    <tbody>

                        <tr>
                            <td style="text-align: left;" width="100%"><strong>Question '. ($i+1) .'</strong></td> 
                        </tr>
                        <tr>
                            <td style="text-align: left;" width="100%">' . $question_and_answers[$i]['question_name'] .'</td>
                        </tr>



                        <tr>
                            <td style="text-align: left;" width="100%" class="warning"><em>Question Not attempted</em><br><strong>Correct Answer is</strong><br>' . $numberofanswersperquestion[0]['answer_name'] . '</td>
                        </tr>

                        <tr>
                            <td style="height: 5px;" width="100%">&nbsp;</td>
                        </tr>

                        <tr>
                        <td style="height: 5px;" width="100%">&nbsp;</td>
                        </tr>

                    </tbody>
                    </table>';

 }

大家好我有这个for循环,我想做的是,只打印这个

<tr>
    <td style="text-align: left;" width="100%" class="warning"><em>Question Not attempted</em><br><strong>Correct Answer is</strong><br>' . $numberofanswersperquestion[0]['answer_name'] . '</td>
</tr>

基于if语句。但是如何把那个if语句放在那个echo中?

我试过这种方式,

' . if() {} . '

但我无法以这种方式工作。

这就是我想要的if语句

if($student_answer_per_question === '' || '') {
 <tr>
    <td style="text-align: left;" width="100%" class="warning"><em>Question Not attempted</em><br><strong>Correct Answer is</strong><br>' . $numberofanswersperquestion[0]['answer_name'] . '</td>
</tr>
}

我希望我已经详细解释了这个问题,感谢你们的帮助。感谢

2 个答案:

答案 0 :(得分:1)

您可以临时定义变量来存储输出。

$output = '<table class="table table-bordered table-condensed table-datatable table-hover">
                <tbody>

                    <tr>
                        <td style="text-align: left;" width="100%"><strong>Question '. ($i+1) .'</strong></td> 
                    </tr>
                    <tr>
                        <td style="text-align: left;" width="100%">' . $question_and_answers[$i]['question_name'] .'</td>
                    </tr>';
if(condition)
{
$output .= '             <tr>
                        <td style="text-align: left;" width="100%" class="warning"><em>Question Not attempted</em><br><strong>Correct Answer is</strong><br>' . $numberofanswersperquestion[0]['answer_name'] . '</td>
                    </tr>';
}
$output .=              '<tr>
                        <td style="height: 5px;" width="100%">&nbsp;</td>
                    </tr>

                    <tr>
                    <td style="height: 5px;" width="100%">&nbsp;</td>
                    </tr>

                </tbody>
                </table>';

答案 1 :(得分:-1)

在echo语句之前创建一个变量,例如 $ statement ,并使用PHP Shorthand If / Else,类似这样的

$statement = ($student_answer_per_question == '')? "Option 1" : "Option 2";

现在您可以在echo语句中打印 $ statement ,如

echo "blah blah $statement blah blah";
相关问题