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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?