10

I know in php you can embed variables inside variables, like:

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

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

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

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:

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

Or I want dynamic changes in that 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 actually not so awesome..") 
            because somefunc("won\'t work due to my problem.")
        </body>
    </html>
EOF;
?>

Well?

1
  • 1
    <? $var1 = "I\'m including {$var2} in this variable.."; ?> Why are you escaping a single quote inside double quotes? ;) Commented Sep 13, 2008 at 9:08

3 Answers 3

25

Function calls within strings are supported since PHP5 by having a variable containing the name of the function to 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 it easier however (and this works in PHP4) to either just call the function outside of the string:

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

or assign to a temporary variable:

<?
$bar = somefunc("bar");
echo "foo {$bar} baz";
?>
Sign up to request clarification or add additional context in comments.

2 Comments

I guess that works, but I'd never do that. It's screaming bad practice. Think about code readability etc.. Come back to your code in six months and figure out all the function calls inside your HTML markup.
You should never call a function inside your view anyway except for escaping functions like htmlspecialchars (which is a way too long function name for it's purpose, screw you, PHP)…
2

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

Comments

-1

Expanding a bit on what Jason W said:

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";
?>

You can also just embed this function call directly in your html, like:

<?

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 = "\n
\n"; $body = "Today is: " . get_date() . "\n"; } ?> <html> <head> <title><? page_title(); ?></title> </head> <body> <? page_body(); ?> </body> </html>

3 Comments

example assumes you have short tags enabled
I also think you mean $body .= ", World!"; (dot before equals). Just in case someone copies/pastes your example...
The page_title function echoes something so wouldn't work with short tags. // The page_body function returns nothing so it would do nothing with short tags enabled. // Also, even if it would return something, <? and <?= are a very big difference, where <? does nothing // This is very bad code!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.