首页 > 解决方案 > 如何在php中的if语句之外使用变量

问题描述

我正在尝试根据月份比较两种不同的产品价格。目前,我想使用 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;
}

标签: php

解决方案


推荐阅读