Solved

Php If statement

Posted on 2009-05-15
8
413 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
8 Comments
 
LVL 108

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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

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 108

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
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.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now