简体   繁体   English

如何在php中打印此返回值?

[英]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);

Im trying to get my returned value to print but it never seems to work. 我试图打印返回的值,但它似乎永远无法正常工作。

How about saving the return of the function in a variable and printing the variable 如何将函数的返回值保存在变量中并打印变量

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);

if you are returning something you must have to catch it in some variables. 如果要返回某些内容,则必须将其包含在某些变量中。 Also in PHP you need to use echo to print the variables. 同样在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;

Just echo it 只是回声

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);

You want this: 你要这个:

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); } $ msg = printmsg(“ hello”,1);

//lots of other code print($msg); //很多其他代码print($ msg);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM