Solved

Php If statement

Posted on 2009-05-15
8
423 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

617 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