【问题标题】:How to include an if-statement inside a PHP echo?如何在 PHP 回显中包含 if 语句?
【发布时间】:2016-04-06 10:42:28
【问题描述】:
 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>
}

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

【问题讨论】:

标签: php html if-statement echo


【解决方案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>';

【讨论】:

  • 做了一些改动后,现在是这个样子,gyazo.com/cad66adb763d2ffc23b71b5a9c500231
  • 你有什么想法吗,否则为什么不被调用? if 语句现在正在工作,但 else echo 根本没有被调用
  • 谢谢我修好了,就是那个if语句,应该是这样的 if($student_answer_per_question == '')
【解决方案2】:

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

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

现在你可以在你的 echo 语句中打印 $statement,就像

echo "blah blah $statement blah blah";

【讨论】:

    猜你喜欢
    • 2013-11-13
    • 2016-03-05
    • 1970-01-01
    • 2013-10-18
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 2023-02-26
    • 2015-02-09
    相关资源
    最近更新 更多