![](/img/trans.png)
[英]How to write custom “or” condition in “where” part; zf2 ZendFramework
[英]how to write the if condition part?
有一些如下链接
https://example.com/cart.php
https://example.com/cart.php?gid=1
https://example.com/cart.php?gid=2
https://example.com/cart.php?a=view
https://example.com/cart.phpcart.php?a=confproduct&i=0
.......
我想在这些链接页面上添加一些文本。 如何写if条件?
if($_SERVER['REQUEST_URI']=='/cart.php'||......)
有没有简单的方法可以做到这一点?
字符串比较怎么样?
$mystr = "https://example.com/cart.php";
if (0 != strncmp($_SERVER['REQUEST_URI'], $mystr, strlen($mystr)) { ... }
strncmp只比较前n个字符; 我们通过strlen
传递计数。
将它们放置在数组中,然后使用in_array()
。
$pages = array(
'/cart.php',
'/cart.php?gid=1',
'/cart.php?gid=2',
'/cart.php?a=view'
);
if(in_array($_SERVER['REQUEST_URI'], $pages)) {
// ...
}
使用$_SERVER['PHP_SELF']
,如下所示:
if($_SERVER['PHP_SELF'] == "/cart.php"){
// do stuff
} elseif($_SERVER['PHP_SELF'] == "/anotherpage.php"){
// do other stuff
}
如果您这样的链接不是太多,则可以使用@zhuanzhou说的方法。
否则,为什么不向uri添加新参数,例如
https://example.com/cart.php?rule=1
https://example.com/cart.php?gid=1&rule=1
https://example.com/cart.php?gid=2&rule=1
https://example.com/cart.php?a=view&rule=1
然后,您的代码可以是:
if($_GET['rule'] === 1){
// something here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.