Solved

Can somebody explain the result of this if-statement?

Posted on 2014-12-08
6
83 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
[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
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 143

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 143

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 110

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 …

752 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