开发者

Is that possible to expand function call in PHP string?

I tried to call foo() inside a string like this:

echo "This is a ${foo()} car";

function foo() {
    return "blue";
}

but, it ends up with a syntax error.

I found here something similar开发者_JAVA百科, but not exactly what I need:

echo "This is the value of the var named by the return value of getName(): {${getName()}}";

Is that possible to do this ?


Is it possible? No.

Functions, method calls, static class variables, and class constants inside {$} work since PHP 5. However, the value accessed will be interpreted as the name of a variable in the scope in which the string is defined. Using single curly braces ({}) will not work for accessing the return values of functions or methods or the values of class constants or static class variables.

It's the first note for the curly syntax on http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex.


Not like that, no... you could:

echo "$(" . foo() . ");";


No, as far as I know you can only do:

echo "hello ".foo();

function foo() {
    return "world";
}

In your example, this would work:

$name = "captaintokyo";
echo "This is the value of the var named by the return value of getName(): {${getName()}}";

function getName()
{
    return "name";
}

// Output:
// This is the value of the var named by the return value of getName(): captaintokyo


Yes, it is possible.

$func = function($param) { return $param; };
function foo($color) { return "$color car"; }

echo "This is a {$func(foo('blue'))}<br/>";

$title = 'blue car';
echo "This is a {$func(str_replace('blue', 'red', $title))}<br/>";

$func() will be called as a function, and the expression in the parentheses will be evaluated as any other PHP code. That way you can directly call almost any function inside string.


You might as well do this:

echo foo();


Wrap the $ within single quotes so it does not isn't to me literal, and use concatenation to append the value of foo

echo '$' . foo();

function foo() {
    return "hello";
}

prints : $hello


The example you gave:

function foo() {return "hello";}
echo "${foo()}";

This example won't work because using functions in PHP's {$} format only allows you to access the result as a variable name and then show the contents of that variable.

In your example, the echo statement will be trying to echo the contents of a variable named $hello.

To see why it isn't working, we need to modify the example:

function foo() {return "hello";}
$hello="world";
echo "${foo()}";

This program will print world. It seems that this is what you've found in the second example you gave.

If you're hoping to get a program that will print hello in your example, you won't be able to do it using this technique. The good news, however, is that there are plenty of much easier ways to do it. Just echo the function return value.


${} syntax looks like shell command substitution, for PHP you could use eval, http://php.net/eval

But be warned eval is evil... You must be sure you know what you pass on to eval.

Call it like this:

<?php echo eval("foo();");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜