-2

I am curious why is the "contains" forumula so complex in PHP?

I am learning PHP coming from a not very used language called Lasso. PHP and Lasso are quite similar.

One thing Lasso has that's really nice is the contains string function >>

if($myVar >> 'x')  // this is a contains statement

Is there an easier way than:

if(strpos($a, 'are') !== false)

It almost seems like a double negative. Why not == True? This is probably the most confusing thing I have come across. I hope someone can shed some light!

EDIT: I think the answer to my question is there is no other shortcut code for contains. Thanks for everyone's help.

Marc Pope
  • 335
  • 1
  • 14
  • What's wrong with `if(strpos($a, 'are') >= 0) { echo 'found'; }`? – Camilo Dec 03 '17 at 03:45
  • 1
    Related: https://stackoverflow.com/questions/4192025/strpos-issue-with-0-false – mickmackusa Dec 03 '17 at 03:47
  • 1
    @Camilo When `strpos` returns `false` meaning "not found", your code will say "found" because `0 == false` is true in PHP. Try running `$a = 'nope'; if(strpos($a, 'are') >= 0) { echo 'found'; }` and see. – ceejayoz Dec 03 '17 at 03:48
  • Remember PHP is Loosely typed. Which is a double edged sword. Its one thing I both love and hate about it. all these are false and then some `0`, `'0'` `false` `null` `''` and `[]` as you can see in this PHP sandbox http://sandbox.onlinephpfunctions.com/code/55ad6b5ba7d93f098c78b2eb236b5044be464eac – ArtisticPhoenix Dec 03 '17 at 03:49
  • 1
    I was really asking why PHP doesn't have a more simple function. It doesn't seem to follow the format of == or != or > < etc. Seems like it could use another symbol. I also don't understand why so many downvotes. It was mostly a question of language structure. – Marc Pope Dec 03 '17 at 04:20
  • There is a simpler function: `strstr`. It's just commonly eschewed for microperformance reasons. – mario Dec 03 '17 at 04:25
  • A tip is to always check the manual and glance the section on return values for Php functions. – Progrock Dec 03 '17 at 05:49
  • If you edit your question, people who downvoted will be able to change their votes. – Camilo Dec 04 '17 at 16:35

2 Answers2

2

As the docs say:

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE.

and

Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

You can't do == true because strpos can return 0 if the string occurs at the 0th index (i.e. the very beginning of the string), which would fail the == true check as 0 is a false-y value.

TL;DR: strpos is not a "contains" function, it's a "what's the position of the string" function that can be used as a "contains" function as a result.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • if it was not a 0 based index it would be confusing when compared to most other things in this language an others. The issue comes from the loosely typed nature of PHP. But PHP is mainly for the web and any data submitted from the web is string type. If PHP was strongly typed then there would be so much more work involved in type casting numbers and what not. To be honest 80% of web design does not involve doing calculations that require numbers. – ArtisticPhoenix Dec 03 '17 at 03:46
1

It should be noted that strpos will do string conversion on objects that contain a __toString() method

such as this

    //Enter your code here, enjoy!

class foo{

    public function __toString(){
        return 'hello world';
    }
}

$Foo = new foo;

echo strpos($Foo, "world");

Ouputs

6

Granted this is a really unlikely scenario, in this case, but I thought it worth mentioning.

As seen in this sandbox

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38