Solved

PHP boolean testing question

Posted on 2014-12-08
3
87 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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
Comment Utility
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 142

Expert Comment

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now