Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a bug in PHP

Tags:

string

php

<?php

if(stripos('http://cp.uctorrent.com', 'cp.utorrent.com') >= 0){
    echo "Good1";
}else{
     echo "Bad1";
}

if(stripos('http://uctorrent.com', 'cp.utorrent.com') >= 0){
    echo "Good2";
}else{
    echo "Bad2";
}

?>

output is

Good1Good2

whereas it should be

Good1Bad2

like image 609
Wasim A. Avatar asked May 08 '26 22:05

Wasim A.


1 Answers

<?php
  if(false >= 0) echo "Good";
  else echo "Bad";
  // this code prints Good
?>

It's not a bug, it's a "weird" boolean conversion.

stripos returns false when the string is not found, and false converts to 0 in PHP.

Directly from the documentation (the problem is the other way around) :

Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

like image 174
krtek Avatar answered May 11 '26 10:05

krtek