简体   繁体   English

在HTML标记内使用PHP

[英]using PHP inside HTML tags

I'm in the middle of learning PHP right now and I'm stuck on this: 我现在正在学习PHP,正处于这一阶段:

<?php
$links= array();
links[0]="https://www.google.co.in/";
links[1]="http://www.reddit.com/";
$n= rand(0,1);
$select= $links[$n];
?>
<body>
  <a href="<?php echo $select; ?>">Random</a>
</body>

I want the page to redirect to either google or reddit randomly, but I don't understand what the problem is. 我希望页面随机重定向到google或reddit,但是我不明白问题是什么。 Any solutions? 有什么办法吗?

Missed $ for links variable ...Change code as 缺少$的链接变量...将代码更改为

links[0]="https://www.google.co.in/";
links[1]="http://www.reddit.com/";

to

$links[0]="https://www.google.co.in/";
$links[1]="http://www.reddit.com/";
<?php
$links= array();
$links [0] = "https://www.google.co.in/";
$links [1] = "http://www.reddit.com/";
$n = rand (0, 1);
$select = $links [$n];

header ("Location: $select");
?>

If you want to redirect automatically: 如果要自动重定向:

1- Use HTML META tag 1-使用HTML META标签

<meta http-equiv="refresh" content="0;URL='<?php echo $select; ?>'">

2- or use PHP Header 2-或使用PHP标头

header("Location: $select");
exit();

Full code: 完整代码:

<?php
$links= array();
$links [0] = "https://www.google.co.in/";
$links [1] = "http://www.reddit.com/";
$n = rand (0, 1);
$select = $links [$n];
header("Location: $select");
exit();
?>
<?php
$links = array(
  "https://www.google.co.in/",
  "http://www.reddit.com/",
// ...
);
$randomLink = $links[rand(0, count($links)-1)];
header("Location: {$randomLink}");
exit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM