Solved

Explain php if statment

Posted on 2010-08-31
8
305 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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
 

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 109

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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

803 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