这只是一个例子,但我可以解决这个问题吗?
function echoText($text){
echo $text;
}
$text2 = echoText("Text");
echo "<h1>$text2</h1><br><h2>$text</h2><h3>$text</h3>";
但结果不是<h1>
,<h2>
或者<h3>
,它只是简单的文本。
这只是一个例子,但我可以解决这个问题吗?
function echoText($text){
echo $text;
}
$text2 = echoText("Text");
echo "<h1>$text2</h1><br><h2>$text</h2><h3>$text</h3>";
但结果不是<h1>
,<h2>
或者<h3>
,它只是简单的文本。
Your function is not returning the value, but echoing it out.
Try
function echoText($text){
return $text;
}
如果我了解您要正确实现的目标,则需要:
function echoText($text)
{
return '<h1>'. $text .'</h1>';
}
然后你可以使用它:
$text2 = echoText('test');
echo $text2;
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.
$text2
contains nothing (well, null
technically), because echoText()
returns nothing.
return
a value from echoText()
or otherwise assign a value to $text2
.