-1

这只是一个例子,但我可以解决这个问题吗?

function echoText($text){
    echo $text;
}

$text2 = echoText("Text");
echo "<h1>$text2</h1><br><h2>$text</h2><h3>$text</h3>";

但结果不是<h1><h2>或者<h3>,它只是简单的文本。

4

4 回答 4

6

Your function is not returning the value, but echoing it out.

Try

function echoText($text){
    return $text;
}
于 2012-06-07T13:15:17.270 回答
2

如果我了解您要正确实现的目标,则需要:

function echoText($text) 
{
    return '<h1>'. $text .'</h1>';
}

然后你可以使用它:

$text2 = echoText('test');
echo $text2;
于 2012-06-07T13:15:03.073 回答
1

I think you mean this:

<?php
function echoText($text){
    echo $text;
}
$text2 = echoText("Text");
echo "<h1>".$text2."</h1><br><h2>".$text."</h2><h3>".$text."</h3>";

?>

You also need a return in your function.

于 2012-06-07T13:16:14.587 回答
1

$text2 contains nothing (well, null technically), because echoText() returns nothing.
return a value from echoText() or otherwise assign a value to $text2.

于 2012-06-07T13:15:39.550 回答