doumixiang2227 2011-10-28 06:58
浏览 17

IF声明:什么更有效? 许多小的,或几个大的?

I have form for my recipe app that inserts ingredients into a db. If nothing has been submitted yet, nutritional values display '0'. Otherwise they update to the post values.

My structure for the nutritional display is:

Calories:
  <?php if ($ingredientArray[calorieKey] >= 1)
  echo $ingredientArray[calorieKey];
    else echo 0; ?><br />
Protein:
  <?php if ($ingredientsArray[proteinKey] >= 1)
  echo $ingredientArray[proteinKey];
    else echo 0; ?><br />

...and continues for about 20 items. The question is:

would it be more efficient to refactor the code using just 1 if/else statement to display 2 different forms (dynamic and static), or is it better as is?

  • 写回答

4条回答 默认 最新

  • duanjing7459 2011-10-28 07:06
    关注
    <?php echo $ingredientArray[calorieKey] >= 1 ? $ingredientArray[calorieKey] : 0 ?>
    

    Looks better.

    Stop worrying about it, and take care of the code purity.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度