[英]Echo variable first and assign variable values later in php
I am echoing the $indicator before $total using if else condition.我使用 if else 条件在 $total 之前回显 $indicator。
But I am getting php error但我收到 php 错误
Notice: Undefined variable: indicator.注意:未定义变量:指标。
How I can achieve this without getting any error and get my result which will be yellow according to the if else condition.我如何在没有任何错误的情况下实现这一点,并根据 if else 条件得到我的结果,该结果将是黄色的。 Is it possible?是否可以?
<?php
$total=6;
echo $indicator;
echo $total;
if($total<5) {
$indicator="red";
} else if($total>7) {
$indicator="green";
} else {
$indicator="yellow";
}
?>
Why don't you try like this你为什么不试试这个
UPDATE更新
<?php
echo myFun();
function myFun(){
$total=6;
if($total<5){
$indicator="red";
}elseif($total>7){
$indicator="green";
}else{
$indicator="yellow";
}
return $indicator = $indicator.$total;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.