Solved

Can somebody explain the result of this if-statement?

Posted on 2014-12-08
6
78 Views
Last Modified: 2014-12-09
$server = "localhost";

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

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

Open in new window


Can somebody please explain to me why, regardless of whether the $needle is "ddd" or "localhost", the TRUE block of the IF statement is the one executing?
0
Comment
Question by:elepil
6 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40488288
BOTH True statements execute you mean? You have two separate if statements there.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40488291
please refer to the explanation in your previous question, and again to this page:
http://php.net/manual/en/types.comparisons.php

only === will match on the data type (first) AND then on the value.
so, if the data type does not match (boolean is not a number), it will not match, even if with == the implicitly converted values would match
0
 

Author Comment

by:elepil
ID: 40488497
As more experienced PHP developers than I am, can you show me the actual code you would do to test strpos() whether or not it successfully found the substring? That is really all I'm trying to do. Being provided a link to a long tutorial article that covers more than what I'm trying to accomplish is frustrating and will not get credit.

I am accustomed to other classic OOP languages like Java, and it frustrates me that something this simple is not simple in PHP.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 40488510
as in your other question:
$server = "localhost";

$match1 = strpos($server, "ddd");
$match2 = strpos($server, "localhost");


            if ( $match1 === FALSE) {
                echo "Substring1  was not found<br/>";
            }
            else {
                echo "Substring1 was found<br/>";
            }

           if ( $match2 === FALSE) {
                echo "Substring2  was not found<br/>";
            }
            else {
                echo "Substring2 was found<br/>";
            }

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 40488537
PHP is a loosely typed language.  The references in the comments explain what you need to know, and they are required understandings for PHP developers.  There are different ways to compare data strings -- equality and identicality.  "Equality" says that the data are functionally equivalent under the rules of loose data typing where, for example, FALSE equals NULL equals zero, equals an empty string, etc.  "Identicality" says that the data are equivalent in both function and form.  In these comparisons, both the functional value and the data type are taken into account.  

Because of PHP type juggling, a boolean FALSE is == a numeric zero with loose comparison.  

Because of strict comparison, a boolean FALSE is not === a numeric zero when both value and type are considered.

The rules change in object-oriented PHP, where they are more like Java.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12310-PHP-Variables-and-References.html

Please see: http://iconoun.com/demo/temp_elepil.php

<?php // demo/temp_elepil.php
error_reporting(E_ALL);

/**
 * SEE: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28576850.html
 *
 * REF: http://php.net/manual/en/language.types.php
 * REF: http://php.net/manual/en/types.comparisons.php
 * REF: http://php.net/manual/en/language.types.type-juggling.php
 *
 * REF: http://php.net/manual/en/function.strpos.php
 */


// A TEST DATA SET: A VALUE IN A STRING VARIABLE
$server = "localhost";


/**
 * THE WRONG WAY WITH LOOSE DATA TYPING
 */
echo "WITH LOOSE DATA TYPE COMPARISONS:<br>";

// DOES THE $server VARIABLE CONTAIN 'ddd'?
if (strpos($server, "ddd") >= 0)
{
    echo "Substring 'ddd' was found<br/>";
}
else
{
    echo "Substring 'ddd' was NOT found<br/>";
}

// DOES THE $server VARIABLE CONTAIN 'localhost'?
if (strpos($server, "localhost") >= 0)
{
    echo "Substring 'localhost' was found<br/>";
}
else
{
    echo "Substring 'localhost' was NOT found<br/>";
}


/**
 * THE RIGHT WAY - DISTINGUISH BETWEEN FALSE AND ZERO
 */
echo "WITH STRICT DATA TYPE COMPARISONS:<br>";

// DOES THE $server VARIABLE CONTAIN 'ddd'?
if (strpos($server, "ddd") === FALSE)
{
    echo "Substring 'ddd' was NOT found<br/>";
}
else
{
    echo "Substring 'ddd' was found<br/>";
}

// DOES THE $server VARIABLE CONTAIN 'localhost'?
if (strpos($server, "localhost") === FALSE)
{
    echo "Substring 'localhost' was NOT found<br/>";
}
else
{
    echo "Substring 'localhost' was found<br/>";
}

Open in new window

HTH, ~Ray
0
 

Author Closing Comment

by:elepil
ID: 40489267
Thank you for your responses.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
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 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 …

863 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

22 Experts available now in Live!

Get 1:1 Help Now