html - In PHP is it possible to use a function inside a variable -
i know in php can embed variables inside variables, like:
<? $var1 = "i\'m including {$var2} in variable.."; ?>
but wondering how, , if possible include function inside variable. know write:
<?php $var1 = "i\'m including "; $var1 .= somefunc(); $var1 = " in variable.."; ?>
but if have long variable output, , don't want every time, or want use multiple functions:
<?php $var1 = <<<eof <html lang="en"> <head> <title>aaahhhhh</title> <meta http-equiv="content-type" content="text/html;charset=utf-8"> </head> <body> there <b>alot</b> of text , html here... want <i>functions</i>! -somefunc() doesn't work -{somefunc()} doesn't work -$somefunc() , {$somefunc()} doesn't work of course because function needs string -more non-working: ${somefunc()} </body> </html> eof; ?>
or want dynamic changes in load of code:
<? function somefunc($stuff) { $output = "my bold text <b>{$stuff}</b>."; return $output; } $var1 = <<<eof <html lang="en"> <head> <title>aaahhhhh</title> <meta http-equiv="content-type" content="text/html;charset=utf-8"> </head> <body> somefunc("is awesome!") somefunc("is not awesome..") because somefunc("won\'t work due problem.") </body> </html> eof; ?>
well?
function calls within strings supported since php5 having variable containing name of function call:
<? function somefunc($stuff) { $output = "<b>{$stuff}</b>"; return $output; } $somefunc='somefunc'; echo "foo {$somefunc("bar")} baz"; ?>
will output "foo <b>bar</b> baz
".
i find easier (and works in php4) either call function outside of string:
<? echo "foo " . somefunc("bar") . " baz"; ?>
or assign temporary variable:
<? $bar = somefunc("bar"); echo "foo {$bar} baz"; ?>
Comments
Post a Comment