如何正确地将if条件放入php字符串中

时间:2016-01-11 09:44:41

标签: php html

我有以下代码来打印一些数据,

$html = "<table><tr><th>Email Address</th><th>Event</th><th>Tag</th><th>Time</th></tr>"

    .if(count($result->http_response_body->items) > 0) {
        foreach ($result->http_response_body->items as $key) {.
             "<tr>
             <td>". $key->recipient . "</td>
             <td>". $key->event . "</td>
            <td>" . @$key->tags[0] . "</td>
            <td>" . date("r", $key->timestamp) . "</td>
            </tr>"
        .}

        //fetchLogs($result);
    }. 
     "</table>";

    echo $html;   

当我执行代码时,它会出现语法错误,出现意外情况,如果&#39;条件。如何在此表字符串中插入此if条件和其他变量。请帮忙。

7 个答案:

答案 0 :(得分:1)

你有很多语法错误

$html = "<table><tr><th>Email Address</th><th>Event</th><th>Tag</th>    <th>Time</th></tr>";

if ( count( $result->http_response_body->items ) > 0 ) {
    foreach ( $result->http_response_body->items as $key ) {
        $html .= "<tr>
             <td>" . $key->recipient . "</td>
             <td>" . $key->event . "</td>
            <td>" . @$key->tags[ 0 ] . "</td>
            <td>" . date( "r", $key->timestamp ) . "</td>
        </tr>";
    }

    //fetchLogs($result);
}
$html .= "</table>";

echo $html; 

另外,不要犯错误(@$key->tags[0])。首先使用isset进行正确检查。或者,如果您使用的是php7,则可以使用$key->tag[0] ?? 'something else'之类的null coalesce运算符。

答案 1 :(得分:0)

请试试这个

$html = "<table><tr><th>Email Address</th><th>Event</th><th>Tag</th><th>Time</th></tr>" ;

    if(count($result->http_response_body->items) > 0) {
        foreach ($result->http_response_body->items as $key) {
          $html.=   "<tr>
             <td>". $key->recipient . "</td>
             <td>". $key->event . "</td>
            <td>" . @$key->tags[0] . "</td>
            <td>" . date("r", $key->timestamp) . "</td>
            </tr>";
        }

        //fetchLogs($result);
    } 
     $html.="</table>";

    echo $html;

答案 2 :(得分:0)

首先,PHP语句以;结尾,这是一个分号。

因此,您的第一个陈述需要更正为:

<?php
$html = "<table><tr><th>Email Address</th><th>Event</th><th>Tag</th><th>Time</th></tr>";
if (count($result->http_response_body->items) >0) { // Removed dot
  foreach ($result->http_response_body->items as $key) { // Removed dot
    $html .= "<tr><td>" .$key->recipient ."</td>
      <td>" .$key->event ."</td>
      <td>" .@$key->tags[0] ."</td>
      <td>" .date("r", $key->timestamp) ."</td></tr>";
  } // Removed Dot.
// fetchLogs($result);
}
$html .= "</table>";
echo $html;
?>

答案 3 :(得分:0)

诀窍很简单,你只需要理解字符串连接,一旦掌握了你可以编写像这样的复杂字符串。

this."name of FLV playback instance".fullScreenTakeOver = false;

答案 4 :(得分:0)

代码中存在语法错误。您可以使用此

    $html = "<table><tr><th>Email Address</th><th>Event</th><th>Tag</th>    <th>Time</th></tr>";

    if ( count( $result->http_response_body->items ) > 0 ) {
        foreach ( $result->http_response_body->items as $key ) {
            $html .= "<tr>
                 <td>" . $key->recipient . "</td>
                 <td>" . $key->event . "</td>
                <td>" . @$key->tags[ 0 ] . "</td>
                <td>" . date( "r", $key->timestamp ) . "</td>
            </tr>";
        }


    }
    $html .= "</table>";

echo $html; 

答案 5 :(得分:0)

除了使用可能对大字符串产生性能损失的字符串连接之外,我个人的偏好是使用添加了项目的数组 - 最后内容输出到屏幕。

<?php

    $html=array();
    $html[]="
    <table>
        <tr>
            <th>Email Address</th>
            <th>Event</th>
            <th>Tag</th>
            <th>Time</th>
        </tr>";

    if ( count( $result->http_response_body->items ) > 0 ) {
        foreach ( $result->http_response_body->items as $key ) {
            $html[]= "
            <tr>
                <td>{$key->recipient}</td>
                <td>{$key->event}</td>
                <td>{@$key->tags[ 0 ]}</td>
                <td>" . date( "r", $key->timestamp ) . "</td>
            </tr>";
        }
    }

    $html[]= "</table>";

    echo implode( PHP_EOL, $html);
    unset( $html );
?>

答案 6 :(得分:0)

另一种方法:

<table>
   <tr>
      <th>Email Address</th>
      <th>Event</th>
      <th>Tag</th>
      <th>Time</th>
   </tr>
   <?php if(count($result->http_response_body->items) > 0) : ?>
       <?php foreach ($result->http_response_body->items as $key):?>
            <tr>
                <td><?php echo $key->recipient; ?></td>
                <td><?php echo $key->event?; ?></td>
                <td><?php echo @$key->tags[0]; ?></td>
                <td><?php echo date("r", $key->timestamp); ?></td>
            </tr>
       <?php endforeach;?>
   <?php endif;?>
</table>