Solved

PHP boolean testing question

Posted on 2014-12-08
3
94 Views
Last Modified: 2014-12-08
I have a question about testing for Boolean values in PHP. Assume that the $server variable contains the string "localhost".

The strpos() function documentation says it "Returns the position of the first occurrence of a string inside another string, or FALSE if the string is not found. Note: String positions start at 0, and not 1."

Can someone show me how you would test the strpos() function whether or not the substring was found? Because I've tried:

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

Open in new window


But whether I set the substring to "boo" or "localhost", it doesn't seem to make a difference. Thanks.
0
Comment
Question by:elepil
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40488255
this is because you need to test with data type, using === instead of ==:
see here: http://php.net/manual/en/language.operators.comparison.php
$a === $b       Identical       TRUE if $a is equal to $b, and they are of the same type.

hence, this will do the job:
if (strpos($server, "boo") === FALSE) {
0
 

Author Comment

by:elepil
ID: 40488269
I find this particularly frustrating in PHP; it is so unorthodox compared to other languages. Can you please explain to me why the following does NOT work? Because it returns the same result:

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

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40488289
this would do, but doesn't exist:
if (strpos($server, "boo") >== 0) {

again, it is about implicit data type conversions
xx == FALSE  will try to make both sides the same data type, in this case xx to a boolean
xx being 0 will evaluated to false ... hence a "false" match compared to 0 using ==  , but will evaluate to false when using === as the data types don't match

 >=  and <=  are having the same issue, but not the same solution (>== does not exist), hence you HAVE to formulate your code to first test using the data type on equality (===), and then (in the else) test on the actual value if needed
$match = strpos($server, "boo");
if ($match === FALSE )
{  print "no match"; }
else
{ print "match on position " . $match; }

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question