php优化if多重嵌套语句

本文分享了一次使用PHP开发ERP系统时遇到的性能瓶颈问题及解决过程。通过将多重if语句改写为switch-case结构,显著提升了系统的响应速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


在做公司erp系统的过程中,遇到了一个需求。

需要读取数据库内不同的表格,并对其表A的字段,与表B的字段进行判断。

会用到多个判断语句。而本人在编写时由于没想太多大部分用的都是if判断语句来筛选。


最终形成多个if语句嵌套。

					if (!isset($factor_divide))
						{
							if($switch_state=='1') 
							{
								case '1':
							echo '<tr><th  colspan="2">' .$find_error. '<a href="https://item.taobao.com/item.htm?id=' .$PriceRow['storelink']. '" target="_blank" title='.$refresh_time['0'].'-'.$refresh_time['1'].'已刷新'.'>' . $PriceRow['sales_type'] . _('Price') .'(' . locale_number_format($PriceRow['storeprice'],2) . ')' . ''.$PriceRow['company_state'].' </a></th>';
							}
								
							if($switch_state=='2') 
							{
echo '<tr><th colspan="2">' .$find_error. '<a href="https://item.taobao.com/item.htm?id=' . $PriceRow['storelink']. '" target="_blank" title='.$refresh_time['0'].'-'.$refresh_time['1'].'更新数据有误'.'>' . $PriceRow['sales_type'] . _('Price') .'(' . locale_number_format($PriceRow['storeprice'],2) . ')' . ''.$PriceRow['company_state'].' </a></th>';}
							if($switch_state=='0') 
							{
echo '<tr><th colspan="2">' .$find_error. '<a href="https://item.taobao.com/item.htm?id=' . $PriceRow['storelink']. '" target="_blank" title='.'未更新'.'>' . $PriceRow['sales_type'] . _('Price') .'(' . locale_number_format($PriceRow['storeprice'],2) . ')' . ''.$PriceRow['company_state'].' </a></th>';}}


在本地运行时并没有出现问题。

在测试服务器上运行时,就会出现页面严重卡顿。分析原因可能是读取数据库过程中多次获取不同表会造成减慢。

修改了很多个地方发现并没有提速。


直到后来想起网上有phper说过switch case速度会快点。所以把if嵌套换成switch case 。就不卡顿了,具体原理虽然还不太明白。

不过以此为鉴,尽量少用多重嵌套。

						if (!isset($factor_divide)) 
						{
							switch ($switch_state) 
							{
								case '1':
							echo '<tr><th  colspan="2">' .$find_error. '<a href="https://item.taobao.com/item.htm?id=' .$PriceRow['storelink']. '" target="_blank" title='.$refresh_time['0'].'-'.$refresh_time['1'].'已刷新'.'>' . $PriceRow['sales_type'] . _('Price') .'(' . locale_number_format($PriceRow['storeprice'],2) . ')' . ''.$PriceRow['company_state'].' </a></th>';
									break;
								case 2:
							echo '<tr><th  colspan="2">' .$find_error. '<a href="https://item.taobao.com/item.htm?id=' . $PriceRow['storelink']. '" target="_blank" title='.$refresh_time['0'].'-'.$refresh_time['1'].'更新数据有误'.'>' . $PriceRow['sales_type'] . _('Price') .'(' . locale_number_format($PriceRow['storeprice'],2) . ')' . ''.$PriceRow['company_state'].' </a></th>';
									break;								
								case 0:
							echo '<tr><th  colspan="2">' .$find_error. '<a href="https://item.taobao.com/item.htm?id=' . $PriceRow['storelink']. '" target="_blank" title='.'未更新'.'>' . $PriceRow['sales_type'] . _('Price') .'(' . locale_number_format($PriceRow['storeprice'],2) . ')' . ''.$PriceRow['company_state'].' </a></th>';
									break;

							}

						}
					    


### Linux Bash 中多重嵌套 If 语句的语法与示例 在 Linux 的 Bash 脚本中,`if` 语句可以被用来实现条件判断。当需要更复杂的逻辑控制时,可以通过嵌套 `if` 语句来完成多层次的条件判断。 #### 嵌套 If 语句的基本结构 以下是嵌套 `if` 语句的标准语法: ```bash if 条件1; then # 当条件1为真时执行的内容 if 条件2; then # 当条件2也为真时执行的内容 执行指令; fi else # 当条件1不成立时执行的内容 执行其他指令; fi ``` 这种结构允许在一个 `if` 判断内部再定义新的 `if` 判断,从而形成多层嵌套关系[^1]。 #### 实际应用中的例子 下面是一个具体的实例展示如何使用三重嵌套的 `if` 结构来进行数值范围分类: ```bash #!/bin/bash read -p "请输入一个整数:" num if [ "$num" -ge 0 ]; then echo "输入的是正数或者零" if [ "$num" -lt 50 ]; then echo "这个数字小于50" if (( $num % 2 == 0 )); then echo "而且这是一个偶数" else echo "而且这是一个奇数" fi elif [ "$num" -eq 50 ]; then echo "这个数字正好等于50" else echo "这个数字大于50" fi else echo "输入的是负数" fi ``` 在这个脚本里: - 首先检测用户输入是否是非负数; - 如果是,则进一步区分其大小是否低于、等于还是超过50; - 对于小于50的部分再次细分检查是否能被2整除即判定奇偶性[^1]。 以上展示了通过逐步细化条件达到精确控制流程的目的。 #### 注意事项 编写嵌套较多的程序需要注意保持良好的缩进习惯以便阅读维护,并且确保每一级结束都有相应的关闭标签如`fi`配对正确防止出现解析错误[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值