Solved

Can somebody explain the result of this if-statement?

Posted on 2014-12-08
6
76 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 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

19 Experts available now in Live!

Get 1:1 Help Now