开发者

PHP stripos problem

<?php
$url = $_SERVER['HTTP_HOST'];
if(stripos($url,'cp.utorrent开发者_StackOverflow中文版.com')===true)echo "cp";
else echo "welcome uctorrent ...";
?>

for http://cp.uctorrent.com and http://www.cp.uctorrent.com output should be

cp

but its both cases its printing

welcome uctorrent...


stripos returns the position of string, which will be an integer. And you are comparing it with === true

So this condition will be false by comparing an integer and a boolean.

You should use this

$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";

Or

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


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

Returns the numeric position of the first occurrence 

So you're getting a number, and you're checking it to be the same type and value as "true". As it is an int, not a boolean, this will always be false.

Don't you mean !== false there?


stripos will never actually return boolean TRUE. It will return the index or boolean FALSE.

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


stripos returns integer, not boolean, when an occurrence is found


Try this:

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

Some notes here:

  • FALSE and TRUE are constants. It's a good habit to use all caps for constants.
  • All code blocks must always have a starting and ending curly braces for readability.
  • stripos is a long-standing pain, always check value with === FALSE
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜