douzhao7014
2016-04-06 10:42
浏览 480
已采纳

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

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

 }

Hi guys I have this for loop, what I am trying to do is, only print this

<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>

based on an if statement. But how can put that if statement inside that echo ??

I have tried this way, by

' . if() {} . '

but I couldn't get it to work this way.

this is what I want inside the if statement

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

I hope I have explained the problem in detail and I appreciate any help from you guys. Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dot_0620 2016-04-06 10:56
    已采纳

    You can temporarily define a variable to store your output.

    $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>';
    
    点赞 打赏 评论
  • doulang9953 2016-04-06 10:56

    Create a variable, before the echo statement for example $statement and use PHP Shorthand If/Else, something like this

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

    Now you can just print the $statement within your echo statement, as

    echo "blah blah $statement blah blah";
    
    点赞 打赏 评论

相关推荐 更多相似问题