drwn65609
2016-09-12 09:06
浏览 63
已采纳

使用php条件语句用echo打印图像

Hey I'm trying to use php to display an image for each day of the week. I've used the date("D") function to do this but my code doesn't seem to be working. Can someone please take a quick look at it?

<?php
$today = date("D");

if ($today == "Sun")
    echo "Today is Sunday" <img src = 'C:\Users\A1\Documents\2016\Semester 2\ISYS 227\Tutorials\images\sun.jpg'/>;
    elseif ($today == "Mon")
        echo "Today is Monday. <img src = 'C:\Users\A1\Documents\2016\Semester 2\ISYS 227\Tutorials\images\mon.jpg'/>;
    elseif ($today == "Tue")
        echo "Today is Tuesday". <img src = 'C:\Users\A1\Documents\2016\Semester 2\ISYS 227\Tutorials\images\tue.jpg'/>;
    elseif ($today == "Wed")
        echo "Today is Wednesday". <img src = 'C:\Users\A1\Documents\2016\Semester 2\ISYS 227\Tutorials\images\wed.jpg'/>;
    elseif ($today == "Thur")
        echo "Today is Thursday". <img src = 'C:\Users\A1\Documents\2016\Semester 2\ISYS 227\Tutorials\images\thur.jpg'/>';
    elseif ($today == "Fri")
        echo "Today is Friday. <img src = 'C:\Users\A1\Documents\2016\Semester 2\ISYS 227\Tutorials\images\fri.jpg'/>;
    elseif ($today == "Sat")
        echo "Today is Saturday. <img src = 'C:\Users\A1\Documents\2016\Semester 2\ISYS 227\Tutorials\images\sat.jpg'/>;
    else    
    echo "I do not know the day of the week.";

?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsft8327 2016-09-12 09:10
    已采纳

    You have closed off your quotes too early

    echo "Today is Sunday" <img src = ''/>;
    

    should be:

    echo "Today is Sunday <img src = ''/>" ;
    

    Removed image source to make it easier to read.

    Also, as pointed out in the comments, use relative path.

    I have tested this and it works.

    Full Code:

    <?php
    $today = date("D");
    
    if ($today == "Sun")
        echo "Today is Sunday <img src = 'images/sun.jpg'/>" ;
        elseif ($today == "Mon")
            echo "Today is Monday. <img src = 'images/mon.png'/>";
        elseif ($today == "Tue")
            echo "Today is Tuesday. <img src = 'images/tue.jpg'/>";
        elseif ($today == "Wed")
            echo "Today is Wednesday. <img src = 'images/wed.jpg'/>";
        elseif ($today == "Thur")
            echo "Today is Thursday. <img src = 'images/thur.jpg'/>";
        elseif ($today == "Fri")
            echo "Today is Friday. <img src = 'images/fri.jpg'/>";
        elseif ($today == "Sat")
            echo "Today is Saturday. <img src = 'images/sat.jpg'/>";
        else    
        echo "I do not know the day of the week.";
    
    ?>
    
    点赞 评论
  • doutao1171 2016-09-12 09:17

    Could this be what you were attempting to do:

    <?php
        $today      = date("D");        
        if ($today == "Sun") {
            echo "Today is Sunday. <img src = 'C:\Users\A1\Documents\2016\Semester 2\ISYS 227\Tutorials\images\sun.jpg'/>
    ";
        }elseif ($today == "Mon") {
            echo "Today is Monday. <img src = 'C:\Users\A1\Documents\2016\Semester 2\ISYS 227\Tutorials\images\mon.jpg'/>";
        }elseif ($today == "Tue") {
            echo "Today is Tuesday. <img src = 'C:\Users\A1\Documents\2016\Semester 2\ISYS 227\Tutorials\images\tue.jpg'/>";
        }elseif ($today == "Wed"){  
            echo "Today is Wednesday. <img src = 'C:\Users\A1\Documents\2016\Semester 2\ISYS 227\Tutorials\images\wed.jpg'/>";
        }elseif ($today == "Thur") {
            echo "Today is Thursday. <img src = 'C:\Users\A1\Documents\2016\Semester 2\ISYS 227\Tutorials\images\thur.jpg'/>";
        }elseif ($today == "Fri") {
            echo "Today is Friday. <img src = 'C:\Users\A1\Documents\2016\Semester 2\ISYS 227\Tutorials\images\fri.jpg'/>";
        }elseif ($today == "Sat") {
            echo "Today is Saturday. <img src = 'C:\Users\A1\Documents\2016\Semester 2\ISYS 227\Tutorials\images\sat.jpg'/>";
        }else {
            echo "I do not know the day of the week.";
        }
    
    点赞 评论

相关推荐 更多相似问题