如何在php中的字符串内写if语句?

时间:2014-05-10 15:54:03

标签: php if-statement

我想在字符串变量中写一个if语句,它是这样的: -

$report_list="";
    $sql = mysql_query("SELECT * FROM `reports` ORDER BY `id` DESC");
    $reports_count = mysql_num_rows($sql);
    if($reports_count>0){
        while($row = mysql_fetch_array($sql)){
            $id = $row['id'];
            $by = $row['by'];
            $title = $row['title'];
            $message = $row['message'];
            $date = strftime("%d- %b- %Y",strtotime($row['date']));
            $reports_list.="<tr>
            <td>$by</td>
            <td>$title</td>
            <td>$date</td>
            <td><a href='view_report.php?id=$id'>View Report Message</a></td>
            <td>
            if(file_exists('reports/$id.xlsm')){
            <a href='reports/$id.xlsm' download>Download Attached File</a>'.
            } else if(file_exists('reports/$id.xlsx')){
            <a href='reports/$id.xlsx' download>Download Attached File</a>'.
            }
            </td>
            </tr>";

我该如何使这段代码有效?我该如何正确编写这些if语句?

5 个答案:

答案 0 :(得分:2)

你想要的是一个三元运算符。我将举一个简化的例子,因此它更适用于其他情况:

$reports_list .= "<tr><td>$by</td>" . (file_exists('reports') ? "<td>show if true</td>" : "<td>show if false</td>") . '</tr>';

答案 1 :(得分:2)

你需要在两者之间进行:

$reports_list.="<tr>
<td>$by</td>
<td>$title</td>
<td>$date</td>
<td><a href='view_report.php?id=$id'>View Report Message</a></td>
<td>";
if(file_exists('reports/$id.xlsm')){
$reports_list.="<a href='reports/$id.xlsm' download>Download Attached File</a>";
} else if(file_exists('reports/$id.xlsx')){
$reports_list.="<a href='reports/$id.xlsx' download>Download Attached File</a>";
}
$reports_list.="</td>
</tr>";

答案 2 :(得分:2)

您可以使用连接和三元运算符<cond> ? <if-true> : <if-false>

来实现
$reports_list .= "<tr>...<td>"
              . (file_exists('reports/$id.xlsm')
                ? "<a href='reports/$id.xlsm' download>Download Attached File</a>" : "")
              . (file_exists('reports/$id.xlsx')
                ? "<a href='reports/$id.xlsx' download>Download Attached File</a>" : "")
              . "</td></tr>";

由您来决定它是否比仅使用多个if语句更具可读性 - 这种方式建议更短和更简单的条件。

答案 3 :(得分:2)

我认为在实际变量中提供此变量内容是一种好习惯。

它更容易阅读;)

$id = $row['id'];
$by = $row['by'];
$title = $row['title'];
$message = $row['message'];
$date = strftime("%d- %b- %Y",strtotime($row['date']));

$link="";
if(file_exists('reports/$id.xlsm')){
   $link="<a href='reports/$id.xlsm' download>Download Attached File</a>";
} else if(file_exists('reports/$id.xlsx')){
   $link="<a href='reports/$id.xlsx' download>Download Attached File</a>";
}

$reports_list.="<tr>
<td>$by</td>
<td>$title</td>
<td>$date</td>
<td><a href='view_report.php?id=$id'>View Report Message</a></td>
<td>$link</td>
</tr>";

答案 4 :(得分:1)

 $report_list="";
    $sql = mysql_query("SELECT * FROM `reports` ORDER BY `id` DESC");
    $reports_count = mysql_num_rows($sql);
    if($reports_count>0){
        while($row = mysql_fetch_array($sql)){
            $id = $row['id'];
            $by = $row['by'];
            $title = $row['title'];
            $message = $row['message'];
            $date = strftime("%d- %b- %Y",strtotime($row['date']));
            $reports_list.="<tr>
            <td>$by</td>
            <td>$title</td>
            <td>$date</td>
            <td><a href='view_report.php?id=$id'>View Report Message</a></td>
            <td>";

            if(file_exists('reports/$id.xlsm'))
         $reports_list.="<a href='reports/".$id.".xlsm' download>Download Attached File</a>";
             else 
          $reports_list.=  "<a href='reports/".$id.".xlsx' download>Download Attached File</a>";


  $reports_list.="</td> </tr>";