duanju9104 2011-09-20 23:51
浏览 17
已采纳

如果然后用较少的重复声明,如何实现这一点?

I have something simple I'm trying to accomplish with less repetition.

By default, I want a div to be shown, however if $x == 1, then check to see if $y != 1, and if $y doesn't, then don't show the block.

However the best I can come up with is the following:

if($x) {

    if($y != 1) {

        echo '<div>display block</div>';        
    }

} else {

    echo '<div>display block</div>';

}

This seems a bit repetitive.

I know I can tweak it a bit and do something like:

$displayBlock = '<div>display block</div>';

if($x) {

    if($y != 1) {

        echo $displayBlock;     
    }

} else {

    echo $displayBlock;

}

But even still, I have a feeling that there is a way to do this whole if if else thing which I can't see right now.

How do you accomplish the above with less if statements? So: if $x != 1 (default), then show the displayBlock. if $x == 1, and $y != 1, then show the display block. If $x == 1 && $y == 1, then do not show the displayBlock.

  • 写回答

4条回答 默认 最新

  • dshdsh2016 2011-09-20 23:52
    关注
    if (!$x || $y != 1) echo $displayBlock;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计