在 HTML 中调用 PHP 函数

Calling PHP function in HTML

我是 Php 的新手。我确定这是一个简单的问题。我正在尝试在 html 文件的标签内调用函数 getDateExpected。我喜欢它 return 作为文本的数字,如下所示。这是怎么做到的?我看到 php 脚本将是 运行 使用某些事件,例如单击按钮,但在这种情况下,没有任何内容被单击。该页面仅被加载。我知道 javascript 使用 { } 但这对 php 是否可行,还是仅用于处理来自表单的请求?

<?php
    //include "landing.php";
    function getDateExpected($desired){
      $date = date ("d");
      if($date << $desired){
        echo $desired - $date;
      }else{
        echo $date-$desired;
      }
    }

?>

<!DOCTYPE html>
<html>
<title>Coming Soon</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
<style>
body,h1 {font-family: "Raleway", sans-serif}
body, html {height: 100%}
.bgimg {
    background-image: url('obg.png');
    min-height: 100%;
    background-position: center;
    background-size: cover;
}
</style>
<body>

<div class="bgimg w3-display-container w3-animate-opacity w3-text-white">
  <div class="w3-display-topleft w3-padding-large w3-xlarge">
    Potion
  </div>
  <div class="w3-display-middle">
    <h1 class="w3-jumbo w3-animate-top">COMING SOON</h1>
    <hr class="w3-border-grey" style="margin:auto;width:40%">
    <p class="w3-large w3-center">getDateExpected(21) days left</p>
  </div>
  <div class="w3-display-bottomleft w3-padding-large">
    <a href="http://www.twitter.com/" target="_blank">Adrian</a>
  </div>
</div>

</body>
</html>

正如 Fred-ii- 评论的那样,您需要 PHP 个标签 改变这个

<p class="w3-large w3-center">getDateExpected(21) days left</p>

至此

<p class="w3-large w3-center"><?php getDateExpected(21);?> days left</p>

并改变这个

  if($date << $desired)

至此

if($date < $desired)

您的 Web 服务器尝试将 .html 作为静态页面执行,因为此扩展与 PHP 语言无关,因此不会使用 PHP 引擎进行解析。 为此,您需要确保该页面具有 .php 扩展名(与 PHP 引擎关联)。
当您想执行 .html 页面时,您可能想在您的网络服务器上激活 mod 重写。
如果您使用的是 Apache,则可以按照 here.
的说明进行操作 然后,您希望指示 Apache 将所有 *.html 请求重定向到相应的 *.php 文件,而不向浏览器显示真正的扩展名 (.php)。为此,您可以按照 here 中的说明进行操作。 这样,用户将在其浏览器中输入 url http://www.example.com/foo.html 和 Web 服务器,一旦收到文件 foo.html 的 GET 请求,将匹配相应的 .php 脚本(名称相同,扩展名不同)并使用 PHP 自动执行页面 foo.php,而不向用户浏览器显示 URL.

中的更改

"Thank you for the assistance Fred! You have fully answered and addressed all the issues but unfortunately I was not able to select your answer as it was only a comment ): If you would make a proper answer I will select yours as correct for future visitors that would want a better understanding – AdrianDevera"

(这是一个迟到的完整概述代码失败的答案,并且根据 OP 的要求 post 它):

您 post 编写的代码存在一些问题。

让我们一一回顾:

if($date << $desired)

<< 字符(在 PHP 中,在评论中看到您来自 C 背景)是按位运算符,不会按照您的期望执行,如 "check if the date is less than x"
( < 小于):

if($date < $desired)

然后我们有这个:

<p class="w3-large w3-center">getDateExpected(21) days left</p>

它的 getDateExpected() 函数没有包含在 php 标签 <?php ?> 中,因此它永远不会是 called/executed,它只是一个现在串;查看您的 HTML 源代码,您会确切地看到这一点,以及未解析的 PHP 代码。

因此,将其更改为以下内容:

<p class="w3-large w3-center"><?php getDateExpected(21) ;?> days left</p>

除此之外,如评论中所见;您显然是在尝试 运行 作为 .html 文件扩展名,根据我留下的评论,看到这个问题时确实想到了这一点:

默认情况下,该文件扩展名不会解析 PHP 指令,.php 将在 PHP 环境中与安装的网络服务器一起解析。

然而,如果您指示您的服务器将它们视为 PHP 并且它受支持,则 运行 .html 文件并非不可能 PHP。

在 php 环境中和实时(本地)服务器上,运行 语法为:

  • http://localhost/file.php 相对于 file:///file.xxx