• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

Explain php if statment

Can someone please explain in detail what these if statements are doing?

What does the three === signs to in the first one, and What does the single & in the second mean?

thanks


$aProfile = getProfileInfo($iId);
    if($aProfile === false)
        return false;

    if(!((int)$aProfile['Role'] & $iRole))
        return false;

Open in new window

0
whatshakin
Asked:
whatshakin
3 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
=== will not only check the "value", but also the data type.

while ("1" == 1) will return true, ("1" === 1 ) will return false
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
0
 
UrbanTwitchCommented:
Whoa, thanks angelll! Didn't know that :P
0
Technology Partners: 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!

 
Tiller79188231Commented:
$aProfile = getProfileInfo($iId);

"==" will parse an integer to a string, and string to integer and compare, ignoring data types
"===" will keep datatype and compare the values

    if($aProfile === false)
        return false;


the "&" is comparing two clauses to the first argument, so it's saying
if there isn't an integer value for $aProfile['Role'] and $iRole is false then return false

    if(!((int)$aProfile['Role'] & $iRole))
        return false;
0
 
whatshakinAuthor Commented:
So the data types would be either and integer, string or something else?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
see this page to know which data types PHP offers:
http://www.php.net/manual/en/language.types.intro.php
0
 
Tiller79188231Commented:
yes... any of the available data types in php... the "==" will try to convert one to the other to see if the resulting values match .. and "===" will determine if the data types and value for that data type matches
0
 
Ray PaseurCommented:
The conversion of integers into strings and vice versa is called "type juggling" and PHP is a loosely typed language.  You will find this to be of considerable significance if you try to use a string for an array index.  PHP will use the non-numeric strings as-is but will convert the numeric strings into integers.  However if you add alphabetic strings to numbers you will get a different kind of result.  It's not "wrong" to do it this way, it just creates some things about the language that drive computer scientists nuts.

When you REALLY CARE about the data type, use the triple-equal notation.  Otherwise, you can use the double-equal in almost every circumstance with good results.

A good book for getting a foundation in PHP is available from SitePoint:
http://www.sitepoint.com/books/phpmysql4/

Best regards, ~Ray
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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