在php / html块中调用函数

时间:2016-12-20 19:05:17

标签: php html

我有一个函数,我想在echo块的段落标记中调用它。我该怎么做呢?

<?php
include("includes/functions.inc.php");
include_once("includes/dbh.php");
$strSQL = "SELECT * FROM tickets";
$rs = mysqli_query($conn, $strSQL);
    while($row = mysqli_fetch_array($rs)) {
    echo  '<li>
           <div class="hk_sup_domian_name">
               <p><?php echo checkTicketStatus();?></p>
           </div>
           <div class="hk_domain_price">
               <span class="ex-price">Your box is on its way</span>
               <span class="active_price">'.$row['stage'].'</span>
               <div class="hk_promot_btn"><a class="hk_btn" href="progress.php">View</a></div>
           </div>
           </li>';
    }

    mysqli_close($conn);
?>

我试过这样调用它,我收到下面的错误。

<p>'checkTicketStatus();'</p> 
  

语法错误,意外&#39; $ checkTicketStatus&#39; (T_VARIABLE),期待   &#39;,&#39;或&#39;;&#39;

4 个答案:

答案 0 :(得分:2)

<ul>
    <div class="coral-wrapper left absolute">
          <p><?php insertFunctionHere();?></p>
    </div>
</ul>

试试这个。

如果需要打印功能

<p><?php echo insertFunctionHere();?></p>

答案 1 :(得分:1)

这样做:

<ul>
    <div class="coral-wrapper left absolute">
        <p><?php echo checkTicketStatus(); ?></p>
    </div>
</ul>

<强>更新

根据您的编辑,您的代码应如下所示:

<?php
    include("includes/functions.inc.php");
    include_once("includes/dbh.php");
    $strSQL = "SELECT * FROM tickets";
    $rs = mysqli_query($conn, $strSQL);
    while($row = mysqli_fetch_array($rs)) {
        ?>
        <li>
        <div class="hk_sup_domian_name">
            <p><?php echo checkTicketStatus(); ?></p>
        </div>
        <div class="hk_domain_price">
           <span class="ex-price">Your box is on its way</span>
           <span class="active_price"><?php echo $row['stage']; ?></span>
           <div class="hk_promot_btn"><a class="hk_btn" href="progress.php">View</a></div>
        </div>
        </li>
        <?php
    }
    mysqli_close($conn);
?>

答案 2 :(得分:1)

您实际上是在尝试这样做:

echo '<p><?php echo checkTicketStatus();?></p>';

PHP并不是那样的。它只是将整个文字字符串回显到输出,浏览器不知道或不关心如何处理<?php ?>标记。

您希望将函数的输出与字符串的其余部分连接起来:

echo '<p>' . checkTicketStatus() . '</p>';

答案 3 :(得分:0)

你可以打破你的字符串并将它连接起来像

<ul>
<?php

echo '<div class="coral-wrapper left absolute"><p>' . checkTicketStatus().'</p></div>';

 ?>
</ul>

或者这样做

<ul>
<div class="coral-wrapper left absolute">
      <p><?php echo checkTicketStatus();?></p>
  </div>
</ul>