duanbo2048 2013-12-16 14:32
浏览 50
已采纳

如何阻止我的IF语句冲突,如何选择优先级?

I'm quite new to PHP and I've been asked to write a script that displays an image based on time. I've deducted that I need to use PHP's native 'date' function for this and up until I was asked to implement bank holidays and Christmas into it, all went well.

I tried to add the Christmas and bank holidays in as a separate if statement but that was when disaster struck and I haven't been able to figure out why it's come to a halt. I've tried using an online PHP validator to no avail.

Please don't be afraid to throw details and jargon at me as I really wish to expand my knowledge of PHP and rely less on the support of web forums in the future!

Here is the code:

<?php

        // Date-time (day of month, month) 

        $d = date('j'); // Day of month: Numeric without leading zeroes
        $m = date('n'); // Month: As above
        $D = date('w'); // Day of week
        $H = date('G'); // Hour of day

        // REGULAR OPENING TIMES
        // Closed before 9am on weekdays
        if ($H < 9 && $D <= 5) :
                $showroom_img = '/media/gbu0/pagehead/showroom_closed.jpg';
        // Open before 5pm (but after 9, as first if statement overrides this) on weekdays
        elseif ($H < 17 && $D <= 5) :
                $showroom_img = = '/media/gbu0/pagehead/showroom_open.jpg';
        // Closed before 10am on Saturdays
        elseif ($H < 10 && $D == 6) :
                $showroom_img = = '/media/gbu0/pagehead/showroom_closed.jpg';
        // Open between 10am and 2pm on Saturdays
        elseif ($H < 14 && $H > 10 && $D == 6) :
                $showroom_img = = '/media/gbu0/pagehead/showroom_open.jpg';
        // Any other time, display closed image :)
        else :
                $showroom_img = = '/media/gbu0/pagehead/showroom_closed.jpg';
        endif;

        // Holidays 2014

        $xmas_starts = ($d == 23 && $m == 12 && $d > 17);
        $xmas_ends   = ($d == 26 && $m == 12);
        $nyd         = ($d == 1 && $m = 1);

        // Easter Friday: April 18
        // Easter Monday: April 21
        // Early Spring B/H: May 5
        // Late Spring B/H: May 26
        // Summer B/H: August 25

        // All bank holidays: 10am - 2pm

        if( date >= $xmas_starts && $day <= $xmas_ends ) :
                $xmas = true;
        elseif( date = $nyd ) :
                $nyd = true;
        else :
                $bankhol = false;
        endif;

        if( $xmas = true ) :
                $showroom_img = "/media/gbu0/pagehead/showroom_xmas.jpg";
        elseif( $bankhol = true && date('F', 'j') = strpos('April 18' || 'April 21' || 'May 5' || 'May 26' || 'August 25') || date('w') = 6 ) :
                $showroom_img = "/media/gbu0/pagehead/showroom_bhol.jpg";
        elseif( date('w') = 7 ) :
                $showroom_img = "/media/gbu0/pagehead/showroom_closed.jpg";
        else :
                $showroom_img = "/media/gbu0/pagehead/showroom_open.jpg";
        endif;

?>
  • 写回答

3条回答 默认 最新

  • dtwy2858 2013-12-16 14:40
    关注

    First of all. I noticed a couple of bugs :

    elseif( date = $nyd ) :
                    $nyd = true;
    

    and

    if( $xmas = true ) :
                    $showroom_img = "/media/gbu0/pagehead/showroom_xmas.jpg";
    

    In PHP, '=' is the assignment operator, used to assign value to a variable. What you need here is the comparison operator, '=='. For example :

    if( $xmas == true ) :
    

    Or you could also do (it's pointed out in a comment already) :

    if( $xmas ) :
    

    Rgarding priority, the if statements are going to be checked in the order you write them in your code. TO ensure that a condition gets the highest priority, that has to be checked first.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示