php优化if多重嵌套语句
来源:互联网 发布:哪里有淘宝小号买 编辑:程序博客网 时间:2024/06/05 09:27
在做公司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;}}
阅读全文
0 0
- php优化if多重嵌套语句
- 关于excel 中IF 语句的多重嵌套
- 多重if语句
- Java学习笔记(条件运算/运算符优先级/条件语句if/if..else/多重if/嵌套if)
- 嵌套if-else与多重~
- C#中多重IF和嵌套IF
- Java语言——else if语句嵌套的优化
- IF语句的嵌套
- if语句的嵌套
- 011 嵌套 if 语句
- 嵌套if语句
- C++ if嵌套语句
- lesson24 if语句嵌套
- 嵌套if语句
- EXCEL中的嵌套if语句
- 嵌套的if-else语句
- thinkphp if判断嵌套语句
- if语句的嵌套问题
- mysql5.5 for linux 安装
- 对于volatile型变量的特殊规则(深入理解Java虚拟机学习笔记)
- 只读事务和没有事务
- jquery获得option的值和对option进行操作
- 复赛模拟试题 Vijos P1873 物品选取 dp(waterfall)
- php优化if多重嵌套语句
- webpack构建与loaders
- 彻底理解js中this的指向
- Java反射
- 设计模式——观察者模式
- jquery获取上传图片的尺寸
- XCode 8.2 CI命令行打包失败
- 设计模式
- Ar 将虚拟东西放在你周围 ,Vr 将你放在虚拟环境中