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);
我试图让我的返回值打印但它似乎永远不会起作用。
答案 0 :(得分: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);
答案 1 :(得分: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;
答案 2 :(得分: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);
答案 3 :(得分: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;
} $味精= printmsg( “你好”,1);
//许多其他代码 打印($ MSG);