PHP stripos问题

时间:2011-03-15 07:22:36

标签: php string

<?php
$url = $_SERVER['HTTP_HOST'];
if(stripos($url,'cp.utorrent.com')===true)echo "cp";
else echo "welcome uctorrent ...";
?>
<{3}}和http://cp.uctorrent.com输出的

应为

  

CP

但它的两种情况都是打印

welcome uctorrent...

5 个答案:

答案 0 :(得分:1)

stripos返回字符串的位置,这将是一个整数。您正在将其与=== true

进行比较

因此,通过比较整数和布尔值,这个条件将是错误的。

你应该使用这个

$url = $_SERVER['HTTP_HOST'];
if(stripos($url,'cp.uctorrent.com')===FALSE)  // here in your code its cp.utorrent.com
   echo "welcome uctorrent ...";
else
   echo "cp";

或者

$url = $_SERVER['HTTP_HOST'];
if(stripos($url,'cp.uctorrent.com'))
    echo "cp";
else 
    echo "welcome uctorrent ...";

答案 1 :(得分:1)

来自:http://php.net/manual/en/function.stripos.php

Returns the numeric position of the first occurrence 

所以你得到一个数字,你正在检查它的类型和价值是否与“真实”相同。因为它是一个int,而不是一个布尔值,所以它总是错误的。

你的意思是!== false吗?

答案 2 :(得分:1)

stripos永远不会实际返回布尔值TRUE。它将返回索引或布尔值FALSE。

http://php.net/manual/en/function.stripos.php

答案 3 :(得分:0)

发现事件时

stripos返回整数,而不是布尔值

答案 4 :(得分:0)

试试这个:

$url = $_SERVER['HTTP_HOST'];
if(stripos($url,'cp.utorrent.com') === FALSE) {
   echo "welcome uctorrent ...";
} else { 
   echo "cp";
}

这里有一些注意事项:

  • FALSE和TRUE是常量。使用全部大写字母是一个好习惯。
  • 为了便于阅读,所有代码块必须始终具有起始和结束花括号。
  • stripos是一个长期的痛苦,总是用=== FALSE
  • 检查值