php优化if多重嵌套语句
在做公司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; } }
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: 关于在函数中定义大数组(局部变量)所导致的问题
- 下一篇: js嵌套php