php - 如何在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;
}
解决方案
推荐阅读
- android - Android gradle build 引起:java.lang.RuntimeException: java.io.IOException: Failed to read manifest
- jquery - 不满足验证条件时避免滚动顶部
- ios - 在 MVVM 中工作时如何组织和命名代码
- javascript - Javascript字符串从随机字符动画到预定义
- docker - 在 Linux Ubuntu 18.04 上安装 Docker 时出错
- spring-boot - lateinit 属性 vkUserRepository 尚未初始化。为什么?
- node.js - NodeJS 护照如何将项目从 Google+ 登录迁移到 Google 登录?
- scala - Spark ML Params 没有定义 setter - 为什么?
- c# - 在索引触发器上停止旋转
- vba - Word VBA搜索某些单词的句子