Solved

Explain php if statment

Posted on 2010-08-31
8
304 Views
Last Modified: 2012-05-10
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
Comment
Question by:whatshakin
8 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 167 total points
ID: 33569135
=== will not only check the "value", but also the data type.

while ("1" == 1) will return true, ("1" === 1 ) will return false
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33569144
0
 
LVL 5

Expert Comment

by:UrbanTwitch
ID: 33569225
Whoa, thanks angelll! Didn't know that :P
0
 
LVL 2

Assisted Solution

by:Tiller79188231
Tiller79188231 earned 167 total points
ID: 33569247
$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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:whatshakin
ID: 33569520
So the data types would be either and integer, string or something else?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33569531
see this page to know which data types PHP offers:
http://www.php.net/manual/en/language.types.intro.php
0
 
LVL 2

Expert Comment

by:Tiller79188231
ID: 33569536
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
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 33571977
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

920 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

16 Experts available now in Live!

Get 1:1 Help Now