En PHP il est possible d'utiliser une fonction dans une variable

Je connais en php, vous pouvez inclure des variables à l'intérieur de variables, comme:

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

Mais je me demandais comment, et s'il était possible d'inclure une fonction à l'intérieur d'une variable.
Je sais que je pourrais juste écrire:

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

Mais que faire si j'ai une variable de type long pour la sortie, et je ne veux pas faire ça à chaque fois, ou je veux utiliser plusieurs fonctions:

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

Ou je veux changements dynamiques qui se chargent de 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;
?>

Bien?

<? $var1 = "I\'m incluant {$var2} dans cette variable.."; ?> Pourquoi êtes-vous échapper à une seule citation à l'intérieur des guillemets? 😉

OriginalL'auteur troskater | 2008-09-13