-1
function printmsg($string,$type) {//type 1 = green, type 2 = red
    if($type == 1) $msg = "<p style=\"color:#00C5CD\">".$string."</p>";
    if($type == 2) $msg = "<p style=\"color:#f7110b\">".$string."</p>";
    return $msg;
}
printmsg("hello",1);

//lots of other code
print($msg);

我试图让我的返回值打印出来,但它似乎从来没有工作过。

4

4 回答 4

3

如何将函数的返回值保存在变量中并打印变量

function printmsg($string,$type) {//type 1 = green, type 2 = red
    if($type == 1) $msg = "<p style=\"color:#00C5CD\">".$string."</p>";
    if($type == 2) $msg = "<p style=\"color:#f7110b\">".$string."</p>";
    return $msg;
}
$msg = printmsg("hello",1);

//lots of other code
print($msg);
于 2012-12-13T05:29:45.877 回答
1

如果您要返回某些东西,则必须在某些变量中捕获它。此外,PHP您需要使用echo来打印变量。

function printmsg($string,$type) {//type 1 = green, type 2 = red
    if($type == 1) $msg = "<p style=\"color:#00C5CD\">".$string."</p>";
    if($type == 2) $msg = "<p style=\"color:#f7110b\">".$string."</p>";
    return $msg;
}
$msg = printmsg("hello",1);

//lots of other code
echo $msg;
于 2012-12-13T05:30:49.093 回答
0

只是回应它

function printmsg($string,$type) {//type 1 = green, type 2 = red
    if($type == 1) $msg = "<p style=\"color:#00C5CD\">".$string."</p>";
    if($type == 2) $msg = "<p style=\"color:#f7110b\">".$string."</p>";
    return $msg;
}
echo printmsg("hello",1);
于 2012-12-13T05:30:36.797 回答
0

你要这个:

function printmsg($string,$type) {//type 1 = green, type 2 = red
if($type == 1) $msg = "<p style=\"color:#00C5CD\">".$string."</p>";
if($type == 2) $msg = "<p style=\"color:#f7110b\">".$string."</p>";
return $msg;

} $msg=printmsg("你好",1);

//很多其他代码 print($msg);

于 2012-12-13T05:30:58.377 回答