Frage

ich in PHP kennen, können Sie Variablen innerhalb von Variablen einzubetten, wie:

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

Aber ich frage mich, wie, und wenn es möglich ist, eine Funktion innerhalb einer Variablen umfassen. Ich weiß, ich könnte nur schreiben:

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

Aber was ist, wenn ich eine lange Variable für die Ausgabe, und ich möchte nicht diese jedes Mal tun, oder ich will mehrere Funktionen verwenden:

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

oder ich möchte dynamische Änderungen in dieser Last von 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;
?>

Nun?

War es hilfreich?

Lösung

Funktionsaufrufe innerhalb von Strings seit PHP5 unterstützt wird durch eine Variable, die den Namen der Funktion enthält, zu nennen:

<?
function somefunc($stuff)
{
    $output = "<b>{$stuff}</b>";
    return $output;
}
$somefunc='somefunc';
echo "foo {$somefunc("bar")} baz";
?>

gibt "foo <b>bar</b> baz".

Ich finde es jedoch einfacher (und das funktioniert in PHP4) entweder direkt vor der Zeichenfolge die Funktion aufrufen:

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

oder zuweisen zu einer temporären Variablen:

<?
$bar = somefunc("bar");
echo "foo {$bar} baz";
?>

Andere Tipps

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

Die Erweiterung ein wenig auf, was Jason W sagte:

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

Du kannst auch nur diesen Funktionsaufruf direkt in Ihrem html, wie:

<?

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>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top