在字符串内部运行函数


Run a function inside of a string

如何在字符串中运行函数,如php:中所示

echo "This page is under construction
      <br/><br/>Current Date: date('l jS 'of F Y')";

我使用了双引号字符串语句,但函数根本没有运行,这是我运行脚本后在屏幕上得到的:

此页面正在构建

当前日期:日期

echo "This page is under construction<br/><br/>Current Date:" . date('l jS 'of F Y');

而你必须做的是:

$date = "date";
echo "This page is under construction<br/><br/>Current Date: {$date('l jS 'of F Y')}";

当然,字符串串联在这里是可取的选择。

PHP不支持在字符串值中嵌入函数。手册中有一个关于字符串和解析的好页面。但是,您可以嵌入变量,也可以将字符串与函数输出连接起来。

您只需要将日期返回连接到字符串:

echo "This page is under construction<br/><br/>Current Date: " . date('l jS 'of F Y')";

在PHP中。用于字符串串联。