如何创建一个if语句来说明这样的话?基本上,如何使用URI类来确定任何段中是否存在值?
$segment = value_of_any_segment;
if($segment == 1{
do stuff
}
Run Code Online (Sandbox Code Playgroud)
我知道这是非常基础的,但我并不完全理解URI类......
你的问题对我来说有点不清楚,但我会尽力帮忙.您是否想知道如何确定特定段是否存在或者是否包含特定值?
您可能已经知道,您可以使用URI类来访问特定的URI段.使用yoursite.com/blog/article/123
作为一个例子,blog
是在第一段,article
是第二区段,以及123
是第3段.您可以访问每个使用$this->uri->segment(n)
然后你可以构造if语句:
// if segment 2 exists ("articles" in the above example), do stuff
if ($this->uri->segment(2)) {
// do stuff
}
// if segment 3 ("123" in the above example) is equal to some value, do stuff
if ($this->uri->segment(3) == $myValue) {
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
希望有所帮助!如果没有,请告诉我,我可以详细说明或提供其他信息.
编辑:
如果您需要确定特定字符串是否出现在 URI的任何段中,您可以执行以下操作:
// get the entire URI (using our example above, this is "/blog/article/123")
$myURI = $this->uri->uri_string()
// the string we want to check the URI for
$myString = "article";
// use strpos() to search the entire URI for $myString
// also, notice we're using the "!==" operator here; see note below
if (strpos($myURI, $myString) !== FALSE) {
// "article" exists in the URI
} else {
// "article" does not exist in the URI
}
Run Code Online (Sandbox Code Playgroud)
关于strpos()的注释(来自PHP文档):
此函数可能返回布尔值FALSE,但也可能返回非布尔值,该值的计算结果为FALSE,例如0或"".有关更多信息,请阅读有关布尔值的部分.使用===运算符测试此函数的返回值.
我希望我的编辑有所帮助.如果我能详细说明,请告诉我.
归档时间: |
|
查看次数: |
15232 次 |
最近记录: |