Solved

Explain php if statment

Posted on 2010-08-31
8
308 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
[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
8 Comments
 
LVL 143

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 5

Expert Comment

by:UrbanTwitch
ID: 33569225
Whoa, thanks angelll! Didn't know that :P
0
Independent Software Vendors: 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 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 143

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 110

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

707 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