douluanji8752 2012-11-07 07:33
浏览 93
已采纳

使用太多if块是好的吗? 什么是可能的替代品? [关闭]

I have been designing a shopping website lately in PHP. Now in Checkout page i have to check many times if the user is logged in. i.e If the user is logged in hide the Signin div and show the Shipping div.

<section class="panels <?php if(!isset($_SESSION['username'])) echo"unactivepanel"; else echo"activepanel";?>" id="Sign In">
        <div class="wrapper1" >
            <ul>
            <li><input type="email" placeholder="Enter your email address" required/></li>
            <li class="inf">(will only be used for sending you order information.)</li>
            <fieldset><legend class="orb"> OR</legend>
            <li><input type="button" value="Sign In to booksmore" class="sb si llb" /></li></fieldset>
            <fieldset><legend class="orb"> OR</legend>
            <li>Sign In using any of following service:</li>
            <li>
                <div class="box1 fc">Facebook</div>
                <div class="box1 tw">Twitter</div>
                <div class="box1 gg">Google</div>
                <div class="box1 oi">Open Id</div>
            </li></fieldset>

            </ul>
        </div>
    </section>
    <section class="panels <?php if(!isset($_SESSION['username'])) echo"unactivepanel"; else echo"activepanel";?>unactivepanel" id="Shipping"></section>
    <section class="panels unactivepanel" id="Confirm"></section>

What i have studied is using too many if block slows down the executions speed, So am I right using too many if blocks ? if I am not coding it right(what i feel right now) then what may be the possible alternatives to if blocks ? (I was thinking of using switch block.)

  • 写回答

2条回答 默认 最新

  • dongyin2643 2012-11-07 07:38
    关注

    If you are using same if else at every place you can easily refactor like.

    <?php 
    if(!isset($_SESSION['username'])) 
     $class = "unactivepanel"; 
    else 
     $class = "activepanel";
    ?>
    

    now simply do echo $class wherever you need it,like

     <section class="panels <?php echo $class;?>unactivepanel" id="Shipping"></section>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教