但是我想知道如何以及是否可以在变量中包含一个"/>

关于html:在PHP中可以在变量内使用函数

关于html:在PHP中可以在变量内使用函数

In PHP is it possible to use a function inside a variable

我知道在php中您可以将变量嵌入变量中,例如:

1
<? $var1 ="I\'m including {$var2} in this variable.."; ?>

但是我想知道如何以及是否可以在变量中包含一个函数。
我知道我可以写:

1
2
3
4
5
<?php
$var1 ="I\'m including";
$var1 .= somefunc();
$var1 =" in this variable..";
?>

但是如果我有一个长变量用于输出,并且我不想每次都这样做,或者我想使用多个函数怎么办:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
$var1 = <<<EOF
    <html lang="en">
        <head>
            AAAHHHHH
            <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        </head>
        <body>
            There is alot of text and html here... but I want some functions!
            -somefunc() doesn't work
            -{somefunc()} doesn't work
            -$somefunc() and {$somefunc()} doesn't work of course because a function needs to be a string
            -more non-working: ${somefunc()}
        </body>
    </html>
EOF
;
?>

或者我想在代码负载中动态更改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?
function somefunc($stuff) {
    $output ="my bold text {$stuff}.";
    return $output;
}

$var1 = <<<EOF
    <html lang="en">
        <head>
            AAAHHHHH
            <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        </head>
        <body>
            somefunc("is awesome!")
            somefunc("is actually not so awesome..")
            because somefunc("won\'t work due to my problem.")
        </body>
    </html>
EOF
;
?>

好吗?


自PHP5以来,通过在变量中包含要调用的函数名称来支持字符串内的函数调用:

1
2
3
4
5
6
7
8
9
<?
function somefunc($stuff)
{
    $output ="{$stuff}";
    return $output;
}
$somefunc='somefunc';
echo"foo {$somefunc("bar")} baz";
?>

将输出" foo bar baz"。

不过,我发现仅调用字符串之外的函数会更容易(这在PHP4中有效):

1
2
3
<?
echo"foo" . somefunc("bar") ." baz";
?>

或分配给一个临时变量:

1
2
3
4
<?
$bar = somefunc("bar");
echo"foo {$bar} baz";
?>

"bla bla bla".function("blub")." and on it goes"


进一步解释Jason W所说的话:

1
2
3
4
5
6
I find it easier however (and this works in PHP4) to either just call the
function outside of the string:

<?
echo"foo" . somefunc("bar") ." baz";
?>

您也可以直接在HTML中嵌入此函数调用,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?

function get_date() {
    $date = `date`;
    return $date;
}

function page_title() {
    $title ="Today's date is:". get_date() ."!";
    echo"$title";
}

function page_body() {
    $body ="Hello";
    $body =",  World!";
    $body ="\
\
"
;
    $body ="Today is:" . get_date() ."\
"
;
}

?>
<html>
    <head>
    <? page_title(); ?>
    </head>
    <body>
    <? page_body(); ?>
    </body>
</html>


推荐阅读

    linux命令和库函数?

    linux命令和库函数?,系统,标准,管理,地址,服务,库函数,环境,平台,第一,状态,1

    linux变量释放命令?

    linux变量释放命令?,系统,环境,名称,工具,官网,简介,变量,环境变量,命令,内

    linux查看函数命令?

    linux查看函数命令?,系统,信息,名称,命令,位置,情况,公用,名字,标准,资料,lin

    linux调出变量的命令?

    linux调出变量的命令?,系统,工作,工具,信息,地址,代码,标准,名称,官网,命令,l

    linux命令主机名变量?

    linux命令主机名变量?,系统,主机名,查询系统,命令,终端,编辑,提示符,根目

    调用函数命令linux?

    调用函数命令linux?,系统,管理,网络,通用,统一,观察,地址,代码,设备,地方,怎

    linux嵌入式截图命令?

    linux嵌入式截图命令?,系统,环境,软件,网络,支柱,代码,工具,标准,发展,工作,l

    嵌入式的linux命令?

    嵌入式的linux命令?,系统,工作,地址,软件,信息,基础,命令,嵌入式,电脑,环境,l

    linux变量是一个命令?

    linux变量是一个命令?,系统,信息,变量,名称,官网,地址,环境,代码,地方,命令,$

    linux中变量取余命令?

    linux中变量取余命令?,地址,工作,系统,数据,信息,命令,分析,目录,控制台,文

    linux查询函数命令?

    linux查询函数命令?,系统,信息,名称,标准,函数,百度,代码,名字,最新,实时,Lin

    嵌入式linux解压命令?

    嵌入式linux解压命令?,系统,环境,工具,产品,网络,嵌入式,软件,基础知识,电

    加载变量的linux命令?

    加载变量的linux命令?,工具,系统,名称,环境变量,环境,命令,用户,文件,变量,

    linux变量赋值命令?

    linux变量赋值命令?,标准,数据,数字,时间,系统,名称,变量,赋值,代码,环境,lin

    linux设置变量的命令?

    linux设置变量的命令?,工具,环境变量,系统,环境,命令,方法,终端,文件,路径,

    linux命令替换变量值?

    linux命令替换变量值?,系统,档案,密码,位置,情况,发行,命令,变量,脚本,内容,L

    嵌入式linux命令语句?

    嵌入式linux命令语句?,系统,环境,基础,网络,软件,基础知识,服务,设备,管理,

    anaconda环境变量配置?

    anaconda环境变量配置?,环境,系统,工具,管理系统,管理,信息,名称,环境变量