【问题标题】:Call function from php page [closed]从php页面调用函数[关闭]
【发布时间】:2017-07-24 17:33:00
【问题描述】:

我正在尝试在我的主页上显示 php 页面中的 html。 我有一个函数可以在名为 home-content.php 的 php 页面中调用一些 html

<?php function HomeFeed ($replStr) {
return <<<HTML
  <div class="wider-width">
   <div class="col-full">

    <table class="homefeed1">
    <tbody>
     <tr>
     <td><center><h1>Store Location</h1>
        <h4>Address Line 1<br>
        Address Line 2<br></h4></br><h2><strong>Hours</strong></h2><br>
        <h4>10 AM to 6:30 PM Monday through Friday</h4><br>
        <h4>9 AM to 2 PM Saturday</h4><br
        <h2>Call Now</h2><h4>(903) 769-8043</h4></center>
     </td>
     </tr>
    </tbody>
    </table>
HTML;
}
?>

现在我有了我的主页模板,我想调用该函数来显示 html。现在,我确信这完全是错误的,但我是尝试使用 php 的新手,所以我已经移动了很多东西。我几乎在某一时刻工作过,html 正在显示,但它也显示 HomeFeed 返回

<?php
require_once('home-content.php');
?>

HomeFeed()

【问题讨论】:

  • 你没有把这个HomeFeed() 包装在php标签中吗?
  • 为什么这个编辑stackoverflow.com/review/suggested-edits/16812697试图修复代码?它应该被拒绝。这是不允许的。编辑:和“此编辑旨在解决帖子的作者,作为编辑没有任何意义。它应该写成评论或答案。”是被拒绝的原因。
  • 其实我还以为是你搞错了
  • 你在php标签之外调用了一个php函数
  • @HenryBbosa “我实际上认为这是你犯的错误” - 可以编辑的错误是文本中的拼写错误,而不是代码。您也可以标记编辑,而不是“代码”,请记住这一点。

标签: php html templates


【解决方案1】:

您需要将函数调用包装在 php 转义标记中。此外,您可能需要考虑修改如下功能。

<?php
    require_once('home-content.php');
    HomeFeed();
?>

<?php function HomeFeed () { ?>

  <div class="wider-width">
   <div class="col-full">

    <table class="homefeed1">
    <tbody>
     <tr>
     <td><center><h1>Store Location</h1>
        <h4>Address Line 1<br>
        Address Line 2<br></h4></br><h2><strong>Hours</strong></h2><br>
        <h4>10 AM to 6:30 PM Monday through Friday</h4><br>
        <h4>9 AM to 2 PM Saturday</h4><br
        <h2>Call Now</h2><h4>(903) 769-8043</h4></center>
     </td>
     </tr>
    </tbody>
    </table>

<?php } ?>

【讨论】:

  • 如果返回 HTML,则不需要 echo - 确实如此。
  • 好的。我会编辑。
  • 我试过了,也没用。
  • 另外,如果有意义,我建议更改函数以回显结果或转义为 html。
  • 它会抛出一个带有 php 标签的错误:computerconceptsofhl.com
【解决方案2】:

试试这个将字符串分配给变量并回显它:

将你的函数调用包装在 php 标签内。

   <?php function HomeFeed () {

$returnstring = <<<HTML
  <div class="wider-width">
   <div class="col-full">

    <table class="homefeed1">
    <tbody>
     <tr>
     <td><center><h1>Store Location</h1>
        <h4>Address Line 1<br>
        Address Line 2<br></h4></br><h2><strong>Hours</strong></h2><br>
        <h4>10 AM to 6:30 PM Monday through Friday</h4><br>
        <h4>9 AM to 2 PM Saturday</h4><br
        <h2>Call Now</h2><h4>(903) 769-8043</h4></center>
     </td>
     </tr>
    </tbody>
    </table>
HTML;
echo $returnstring;
}
?>
    <?php
     require_once('home-content.php');
      HomeFeed();
    ?>

【讨论】:

  • 如果返回 HTML,则无需回显 HTML。
  • 我没有返回它的任务
  • 你在重复这个任务。
  • 是变量内容
  • 没必要。
猜你喜欢
  • 1970-01-01
  • 2011-02-23
  • 1970-01-01
  • 1970-01-01
  • 2012-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多