简体   繁体   中英

Echo variable first and assign variable values later in php

I am echoing the $indicator before $total using if else condition.

But I am getting php error

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. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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