?
Solved

Can somebody explain the result of this if-statement?

Posted on 2014-12-08
6
Medium Priority
?
85 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

719 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