<p>I know in php you can embed variables inside variables, like:</p>

<pre><code>&lt;? $var1 = "I\'m including {$var2} in this variable.."; ?&gt;
</code></pre>

<p>But I was wondering how, and if it was possible to include a function inside a variable.
I know I could just write:</p>

<pre><code>&lt;?php
$var1 = "I\'m including ";
$var1 .= somefunc();
$var1 = " in this variable..";
?&gt;
</code></pre>

<p>But what if I have a long variable for output, and I don't want to do this every time, or I want to use multiple functions:</p>

<pre><code>&lt;?php
$var1 = &lt;&lt;&lt;EOF
    &lt;html lang="en"&gt;
        &lt;head&gt;
            &lt;title&gt;AAAHHHHH&lt;/title&gt;
            &lt;meta http-equiv="Content-Type" content="text/html;charset=utf-8"&gt;
        &lt;/head&gt;
        &lt;body&gt;
            There is &lt;b&gt;alot&lt;/b&gt; of text and html here... but I want some &lt;i&gt;functions&lt;/i&gt;!
            -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()}
        &lt;/body&gt;
    &lt;/html&gt;
EOF;
?&gt;
</code></pre>

<p>Or I want dynamic changes in that load of code:</p>

<pre><code>&lt;?
function somefunc($stuff) {
    $output = "my bold text &lt;b&gt;{$stuff}&lt;/b&gt;.";
    return $output;
}

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

<p>Well?</p>

[{'answer_id': 60420, 'author': 'Jason Weathered', 'author_id': 3736, 'author_profile': 'https://Stackoverflow.com/users/3736', 'pm_score': 6, 'selected': True, 'text': '<p>Function calls within strings are supported since PHP5 by having a variable containing the name of the function to call:</p>\n\n<pre><code>&lt;?\nfunction somefunc($stuff)\n{\n    $output = "&lt;b&gt;{$stuff}&lt;/b&gt;";\n    return $output;\n}\n$somefunc=\'somefunc\';\necho "foo {$somefunc("bar")} baz";\n?&gt;\n</code></pre>\n\n<p>will output "<code>foo &lt;b&gt;bar&lt;/b&gt; baz</code>".</p>\n\n<p>I find it easier however (and this works in PHP4) to either just call the function outside of the string:</p>\n\n<pre><code>&lt;?\necho "foo " . somefunc("bar") . " baz";\n?&gt;\n</code></pre>\n\n<p>or assign to a temporary variable:</p>\n\n<pre><code>&lt;?\n$bar = somefunc("bar");\necho "foo {$bar} baz";\n?&gt;\n</code></pre>\n'}, {'answer_id': 60421, 'author': 'Matthias Winkelmann', 'author_id': 4494, 'author_profile': 'https://Stackoverflow.com/users/4494', 'pm_score': 2, 'selected': False, 'text': '<p><code>"bla bla bla".function("blub")." and on it goes"</code></p>\n'}, {'answer_id': 61270, 'author': 'Tim Kennedy', 'author_id': 5699, 'author_profile': 'https://Stackoverflow.com/users/5699', 'pm_score': -1, 'selected': False, 'text': '<p>Expanding a bit on what Jason W said:</p>\n\n<pre>\nI find it easier however (and this works in PHP4) to either just call the \nfunction outside of the string:\n\n&lt;?\necho "foo " . somefunc("bar") . " baz";\n?&gt;\n</pre>\n\n<p>You can also just embed this function call directly in your html, like:</p>\n\n<pre>&lt;?\n\nfunction get_date() {\n    $date = `date`;\n    return $date;\n}\n\nfunction page_title() {\n    $title = "Today\'s date is: ". get_date() ."!";\n    echo "$title";\n}\n\nfunction page_body() {\n    $body = "Hello";\n    $body = ",  World!";\n    $body = "\\n<br/>\\n";\n    $body = "Today is: " . get_date() . "\\n";\n}\n\n?&gt;\n&lt;html&gt;\n    &lt;head&gt;\n    &lt;title&gt;&lt;? page_title(); ?&gt;&lt;/title&gt;\n    &lt;/head&gt;\n    &lt;body&gt;\n    &lt;? page_body(); ?&gt;\n    &lt;/body&gt;\n&lt;/html&gt;\n\n</pre>\n'}]
['https://Stackoverflow.com/questions/60409', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4867/']