Solved

PHP boolean testing question

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

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Crazy text will not be removed no matter what I try! 13 30
Selecting SUM data from mysql table with PHP 4 19
php simple error message 4 23
PHP Form Calculate Total Price 10 42
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 …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

777 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