?
Solved

PHP boolean testing question

Posted on 2014-12-08
3
Medium Priority
?
97 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

765 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