PHP | strpos() and result compare

Author - Webner

strpos($wholeText, $findText) in PHP returns position from which the $findText value starts if it is present in $wholeText. If it is not found then this function returns false. Fun starts when text is found at the very beginning (at position 0, as 0 is equivalent to false in PHP if not used with correct operator).

Read the code below and comments to understand different cases.

This is the output of the code below:

Not Found without == or ===
Not Found with ==true
Not Found with ===
Not Found with !=false
Found with !==false
Found with ===false
Found with ===FALSE

Code :

<?php
$value = 'This is a long sentence to test search';
$findme = 'This';
$pos = strpos($value, $findme);//$pos will have value 0 as This starts from 0th position in the string
if($pos)
   {
   //since $pos is 0 and 0 is considered as false hence this if condition does not work
   echo 'Found without == or ===';
   }
else
   {
    echo 'Not Found without == or ===';
   }

if($pos == true)
    {
     //$pos is 0 so 0 == true will not work
     echo 'Found with ==true';
    }
    else
    {
      echo 'Not Found with ==true';
    }

if($pos === true)
    {
      //$pos is 0 so 0 === true will not work as both do not match in data type and value
      echo 'Found with ===';
    }
 else
    {
      echo 'Not Found with ===';
    }

if($pos != false)
    {
      //  0 is considered as false hence 0 != false will not work
      echo 'Found with !=false';
     }
else
     {
      echo 'Not Found with !=false';
     }

if($pos !== false)
     {
    //0 !== false works as it matches data type of values as well and they don't match hence this        expression returns true
    echo 'Found with !==false';
      }
 else
      {
        echo 'Not Found with !==false';
       }

if($pos === false)
      {
  //0 === false makes the condition fail as it matches data type of values as well and they don't match hence it returns false and goes to else part hence works
      echo 'Not Found with ===false';
       }
else
       {
        echo 'Found with ===false';
        }

if($pos === FALFALSESE)
        {
          //0 === false makes the condition fail as it matches data type of values as well and they don't match hence it returns false and goes to else part hence works
          echo 'Not Found with ===FALSE';
         }
else
         {
          echo 'Found with ===FALSE';
          }
?>

Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Web development or any other software development assistance please contact us at webdevelopment@webners.com

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *