如何在PHP中的if语句之外使用变量

时间:2020-06-22 09:18:29

标签: php

我正在尝试比较基于月的两种不同产品的价格。目前,我想使用变量 它位于if语句之外。

我确实尝试在JAN语句内添加FEB if语句,但是后来我无法访问FEB if语句。 因此,我认为,如果我可以让变量在if语句之外运行,那么它将起作用。

我也尝试过

$janPrice = 0;

if($result->month == 'JAN')
{
    $janPrice = $result->price;
}

echo $janPrice;

但我只有0;

这是我的代码

if($result->slug == 'product_1')
{
    if($result->month == 'JAN')
    {
        $janPrice = $result->price;
    }

    if($result->month == 'FEB')
    {
        $febPrice = $result->price;
    }

    echo $janPrice;
}

0 个答案:

没有答案