【发布时间】:2019-03-09 19:40:28
【问题描述】:
我想使用一个 URL 将用户重定向到各种传出 URL。例如http://example.com/out.php?ofr=2,其中ofr 将指代用户应重定向到的相应 URL。
我有以下out.php的php代码
这是可以接受的,还是有更有效的方法来完成这个(假设下面的脚本中有 10 个左右不同的 URL)?
<?php
$ofr = $_GET['ofr'];
if ($ofr == 1) {
header('location: http://google.com');
}
elseif ($ofr == 2) {
header('location: http://yahoo.com');
}
else {
header('location: http://msn.com');
}
?>
编辑:按照建议查看 switch 语句,我相信它看起来像:
$ofr = $_GET['ofr'];
switch ($ofr){
case 1: header('location: http://example_1.com');
break;
case 2: header('location: http://example_2.com');
break;
default: header('location: http://example_2.com');
break;
}
看起来正确吗?谢谢!
【问题讨论】:
-
好吧,您可以将位置打包到一个数组中,因此您只需检查一次密钥是否存在,而不是编写大量的
if-statements。 (顺便说一句:switch-statements 确实存在) -
@FranzGleichmann 我编辑了我认为 switch 语句的样子。感谢您的提示。
标签: php