Link to home
Start Free TrialLog in
Avatar of elepil
elepil

asked on

Can somebody explain the result of this if-statement?

$server = "localhost";

            if (strpos($server, "ddd") >= 0) {
                echo "Substring was found<br/>";
            }
            else {
                echo "Substring was NOT found<br/>";
            }

            if (strpos($server, "localhost") >= 0) {
                echo "Substring was found<br/>";
            }
            else {
                echo "Substring was NOT found<br/>";
            }

Open in new window


Can somebody please explain to me why, regardless of whether the $needle is "ddd" or "localhost", the TRUE block of the IF statement is the one executing?
Avatar of Neil Russell
Neil Russell
Flag of United Kingdom of Great Britain and Northern Ireland image

BOTH True statements execute you mean? You have two separate if statements there.
Avatar of Guy Hengel [angelIII / a3]
please refer to the explanation in your previous question, and again to this page:
http://php.net/manual/en/types.comparisons.php

only === will match on the data type (first) AND then on the value.
so, if the data type does not match (boolean is not a number), it will not match, even if with == the implicitly converted values would match
Avatar of elepil
elepil

ASKER

As more experienced PHP developers than I am, can you show me the actual code you would do to test strpos() whether or not it successfully found the substring? That is really all I'm trying to do. Being provided a link to a long tutorial article that covers more than what I'm trying to accomplish is frustrating and will not get credit.

I am accustomed to other classic OOP languages like Java, and it frustrates me that something this simple is not simple in PHP.
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of elepil

ASKER

Thank you for your responses.