Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP boolean testing question

Posted on 2014-12-08
3
Medium Priority
?
99 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 count occurrences of each item in an array.
Suggested Courses

636 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