Is it possible to extend a function call function in a PHP string?

I tried calling foo()inside a line like this:

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

function foo() {
    return "blue";
}

but it ends with a syntax error.

I found here something similar, but not quite what I need:

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

Is it possible to do this?

+3
source share
8 answers

, ... :

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

, , :

echo "hello ".foo();

function foo() {
    return "world";
}

:

$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
+2

, .

$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() , PHP-. , .

+1

:

echo foo();

0

$ , , , foo

echo '$' . foo();

function foo() {
    return "hello";
}

: $hello

0

, :

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

, PHP {$} , .

echo $hello.

, , :

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

world. , , , .

, hello , , . , . .

0

${} , PHP eval, http://php.net/eval

, eval - ... , , eval.

:

<?php echo eval("foo();");
-1

Source: https://habr.com/ru/post/1767185/


All Articles