如何在 PHP echo 中包含 if 语句


How to include an if-statement inside a PHP 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>';
 }

嗨,伙计们,我有这个循环,我想做的是,只打印这个

<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语句放在该回声中?

我试过这种方式,通过

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

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

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

$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>';

创建一个变量,例如在 echo 语句之前$statement并使用 PHP 速记 If/Else,如下所示

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

现在,您只需在 echo 语句中打印$statement,如下所示

echo "blah blah $statement blah blah";