dqp4933 2017-09-29 21:10
浏览 57

PHP - 50%的时间输出2个不同的链接

I am doing a php lab and one of my tasks is to: do a “rick-roll” for a link. You are to output a link ( tag) that contains cat image ( tag). 50% of the time, your code will have the link go to the correct video, the other 50% of the time the link will go to a video of Rick Ashley’s song “Never Going to Give You Up” This task will require an if-else statement.

I know that using an if/else statement using the mt_rand(0,1) function will give me a 50% chance since it will either be 1 or 0 but I don't know how to make the links go to the correct video or Rick Astley's song. Here is my code:

<!-- TASK 2 -- Write your PHP code in in the space provided below.   -->
      <div class="task">
          <h2> Task 2: Rick Roll </h2>
          <p> Click for a funny cat video! </p>
        <? php
            if (mt_rand(0,1) == 0) {
        header('https://www.youtube.com/watch?v=hY7m5jjJ9mM');
        exit;
    } else {
        header('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
        exit;
    }

          <a href="https://www.youtube.com/watch?v=hY7m5jjJ9mM">
    <img src="https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg?h=350&auto=compress&cs=tinysrgb" alt="cute cat" width="250" height="250":>
    </a>
          <!--?php

            /* Task 2: Put your code here */


           ?-->

      </div>
  • 写回答

1条回答 默认 最新

  • douqian6315 2017-09-29 21:23
    关注

    You can save the link in a PHP variable

    if (<your logic here>) {
     $link = "http://link1.com";
    }
    else {
     $link = "http://link2.com";
    }
    

    and using it in HTML <a> tag like this

    <a href='<?php echo $link; ?>'>Link Name</a>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题