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

时间:2015-09-07 13:58:49

标签: php

我的代码

出现语法错误
<?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>';
}
?>

我在第一行收到语法错误。第二个(嵌套的)if语句抛出语法错误。我无法判断出了什么问题。如果我在html之外运行第二个if语句它工作正常。 任何人都可以指出错误吗?

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

尝试:

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

答案 2 :(得分:2)

您正在做的是echo-statement中的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>