php代码中转义html时出现语法错误


Syntax error with Escaping html in php code

我下面的代码有语法错误

<?php 
If (!empty($_SESSION['LogedinStudentId'])) {
echo '<h3>Your Scholarship Applications:</h3>
<table width="100%" class="table table-bordered">
<tr>
<th scope="col">Sr.No.</th>
<th scope="col">Date of Application</th>
<th scope="col">Course Type</th>
<th scope="col">Course Description</th>
<th scope="col">Subject</th>
<th scope="col">Applied for Semester No.</th>
<th scope="col">Scholarship Status</th>
<th scope="col">View / Print</th>
</tr>
<tr>
<td>' . ++$serialno . '</td>
<td>' . if(empty($row_studentdashboard['DateofApplication'])) { 
echo '&nbsp;';
} else {
echo date("d-m-Y", strtotime($row_studentdashboard['DateofApplication']));
};
. '</td>
<td>' . $row_studentdashboard['CourseType'] .'</td>
<td>' . $row_studentdashboard['CourseDescriptionLong'] .'</td>
<td>' . $row_studentdashboard['Subject'] .'</td>
<td>' . $row_studentdashboard['ApplyForSemYear'] .'</td>
<td>' . $row_studentdashboard['ScholarshipStatus'] .'</td>
<td><a href="#">View</a> / <a href="#">Print</a></td>
</tr>
</table>';
} else {
echo '<h3>You do not have any application pending</h4>';
}
?>

我在第一行遇到语法错误。17和22。第二个(嵌套的)if语句抛出语法错误。我判断不出哪里不对。如果我在html之外运行第二个If语句,它工作得很好。有人能指出哪里不对吗?

您所做的是在echo语句中使用if语句。这是错误的。在html外部运行第二个if语句并创建一个变量,稍后在html中打印该变量。一种这样的:

if(empty($row_studentdashboard['DateofApplication'])) { 
$text = '&nbsp;';
} else {
$text = date("d-m-Y", strtotime($row_studentdashboard['DateofApplication']));
}

<td>' . ++$serialno . '</td>
<td>' . $text . '</td>

您不应该将if语句连接到字符串。这就是你在第17/18行所做的

尝试:

<?php 
    if (isset($_SESSION['LogedinStudentId']) && !empty($_SESSION['LogedinStudentId'])) {
        $out = '<h3>Your Scholarship Applications:</h3>';
        $out .= '<table width="100%" class="table table-bordered">';
        $out .= '<tr>';
        $out .= '<th scope="col">Sr.No.</th>';
        $out .= '<th scope="col">Date of Application</th>';
        $out .= '<th scope="col">Course Type</th>';
        $out .= '<th scope="col">Course Description</th>';
        $out .= '<th scope="col">Subject</th>';
        $out .= '<th scope="col">Applied for Semester No.</th>';
        $out .= '<th scope="col">Scholarship Status</th>';
        $out .= '<th scope="col">View / Print</th>';
        $out .= '</tr>';
        $out .= '<tr>';
        $out .= '<td>' . ++$serialno . '</td>';
        $out .= '<td>';
        if(!isset($row_studentdashboard['DateofApplication']) || empty($row_studentdashboard['DateofApplication'])) { 
            $out .= '&nbsp;';
        } else {
            $out .= date("d-m-Y", strtotime($row_studentdashboard['DateofApplication']));
        };
        $out .= '</td>';
        $out .= '<td>' . $row_studentdashboard['CourseType'] .'</td>';
        $out .= '<td>' . $row_studentdashboard['CourseDescriptionLong'] .'</td>';
        $out .= '<td>' . $row_studentdashboard['Subject'] .'</td>';
        $out .= '<td>' . $row_studentdashboard['ApplyForSemYear'] .'</td>';
        $out .= '<td>' . $row_studentdashboard['ScholarshipStatus'] .'</td>';
        $out .= '<td><a href="#">View</a> / <a href="#">Print</a></td>';
        $out .= '</tr>';
        $out .= '</table>';
    } else {
        $out  = '<h3>You do not have any application pending</h4>';
    }
    echo $out;