Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 87
  • Last Modified:

Can somebody explain the result of this if-statement?

$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
elepil
Asked:
elepil
2 Solutions
 
Neil RussellTechnical Development LeadCommented:
BOTH True statements execute you mean? You have two separate if statements there.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
elepilAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
Ray PaseurCommented:
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
 
elepilAuthor Commented:
Thank you for your responses.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now