Solved

Php If statement

Posted on 2009-05-15
8
416 Views
Last Modified: 2012-05-07
What do : and ? mean in if statements?

As in
if($redirect = ($mobile_browser==true) ? $mobileredirect : $desktopredirect){
0
Comment
Question by:walker6o9
[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 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 24399212
It's a true false comparison.

if (statement) ? do this if true : do this if false ;
0
 
LVL 4

Assisted Solution

by:fsouzabrasil
fsouzabrasil earned 100 total points
ID: 24399215
if($redirect = ($mobile_browser==true) ? $mobileredirect : $desktopredirect)

is

if ($redirect = ($mobile_browser==true)){
  $mobileredirect;
}else{
  $desktopredirect;
}
0
 
LVL 11

Assisted Solution

by:climbgunks
climbgunks earned 100 total points
ID: 24399220

this is the conditional operator

<conditional> ? <true value> : <false value>


if conditional is true, the result of the expression is the value before the :,   otherwise the value
after the :

in your statement, the result is assigned to $redirect, and that value is used as true/false in the outer if conditional


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!

 
LVL 9

Assisted Solution

by:wellhole
wellhole earned 50 total points
ID: 24399221
a ? b : c means if a is true, then b, otherwise c.

So, your statement sets $redirect to the results of the browser check. Then if that value is not blank, zero, or null, then it enters the if statement.
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 24399254
Frankly, I find these sort of compound statements to be confusing.  Not sure what is expected to happen after that last curly bracket on the line in the OP.  But it looks to me like the code is trying to set up a redirect string of some sort.
// CHOOSE REDIRECT FOR MOBILE OR DESKTOP
if ($mobile_browser==true)
{
    $redirect = $mobileredirect;
} else
{
    $redirect = $desktopredirect;
}

Open in new window

0
 
LVL 3

Expert Comment

by:markh789
ID: 24400852
It means:
if (STATEMENT) ? STATEMENT_IS_TRUE :  STATEMENT_IS_FALSE)

So it could be

$var = ($hello == world) ? "yes" : "no");

if ($var == "yes") {
 echo "Yay! We got a Yes!";
} else {
 echo "Aw.. We go a No.";
}

It's just a quicker way to write:
$var = ($hello == world) ? "yes" : "no");

Then to write:
if ($hello == world) {
 $var = "yes";
} else {
 $var = "no";
}
0
 

Expert Comment

by:adamjimenez
ID: 24402060
it's called the ternary operator


http://uk.php.net/ternary
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24403696
It's so known for its three parts: (1) statement, (2) true action and (3) false action.

http://dictionary.reference.com/browse/ternary
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…

756 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