Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • Last Modified:

Php If statement

What do : and ? mean in if statements?

As in
if($redirect = ($mobile_browser==true) ? $mobileredirect : $desktopredirect){
0
walker6o9
Asked:
walker6o9
5 Solutions
 
Ray PaseurCommented:
It's a true false comparison.

if (statement) ? do this if true : do this if false ;
0
 
fsouzabrasilCommented:
if($redirect = ($mobile_browser==true) ? $mobileredirect : $desktopredirect)

is

if ($redirect = ($mobile_browser==true)){
  $mobileredirect;
}else{
  $desktopredirect;
}
0
 
Todd MummertCommented:

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
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!

 
wellholeCommented:
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
 
Ray PaseurCommented:
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
 
markh789Commented:
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
 
adamjimenezCommented:
it's called the ternary operator


http://uk.php.net/ternary
0
 
Ray PaseurCommented:
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now