-3

I am trying to find whether or not a string is contained within a string, but it is always true. Why is this always true

<?php

$test = 'ORDER BY `views`';
if(strpos($test,'views') !== true) echo 'true';
else echo 'false';

?>

2 Answers2

3

You're using the parameters for strpos() incorrectly.

You're using strpos($needle, $haystack) but it should actually be strpos($haystack, $needle)

Note that strpos() returns FALSE if the needle was not found in the haystack. So, you'll need to check if it returns FALSE (instead of TRUE).

With your code, it becomes:

if(strpos($test, 'views') !== FALSE) 
    echo 'true';
else 
    echo 'false';

Demo!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
-1

check this

 $test = 'ORDER BY `views`';
   if (strpos($test,'views') !== false) {
        echo 'true';
    } else {
        echo 'false';
    }

also use

You can use these string functions,

strstr — Find the first occurrence of a string

stristr — Case-insensitive strstr()

strrchr — Find the last occurrence of a character in a string

strpos — Find the position of the first occurrence of a substring in a string

strpbrk — Search a string for any of a set of characters

If that doesn't help then you should use preg regular expression

preg_match — Perform a regular expression match

Shakti Patel
  • 3,762
  • 4
  • 22
  • 29