Solved

Php If statement

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

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

752 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