[英]How do I print this returned value in php?
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);
我试图打印返回的值,但它似乎永远无法正常工作。
如何将函数的返回值保存在变量中并打印变量
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);
如果要返回某些内容,则必须将其包含在某些变量中。 同样在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;
只是回声
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);
你要这个:
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);
//很多其他代码print($ msg);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.