1

I have never encountered this in javascript thus far although I am aware : ? means else if. I am having trouble figuring out how to lay it out in PHP. Here is what I did - where am I going wrong if it is wrong?

JSCRIPT

var midparams = dparams2.isramped() ? dparams2 : DailyParams.avg(dparams1, dparams2);

PHP

$midparams = $dparams2->isramped() if $dparams2 = $midparams else $DailyParams->avg($dparams1, $dparams2);
1
  • Where did you get the idea that PHP allows if and else in the middle of an expression like that? Commented Aug 25, 2015 at 3:05

4 Answers 4

1

You can do this:

$midparams = $dparams2->isramped() ? $dparams2 : $DailyParams->avg($dparams1, $dparams2);

Or this:

if($dparams2->isramped()) {
  $midparams = $dparams2;
} else {
  $midparams = $DailyParams->avg($dparams1, $dparams2);
}

You can read more about the ternary operator in:

PHP Manual

Sign up to request clarification or add additional context in comments.

Comments

0

You've got 2 options. 1st one is almost the same as JavaScript

$midparams = ($dparams2 -> isramped()) ? $dparams2 : $DailyParams->avg($dparams1, $dparams2);

The second one is more trivial:

if ($dparams2 -> isramped()) {
    $midparams = $dparams2;
} else {
    $midparams = $DailyParams->avg($dparams1, $dparams2);
}

First one says: midparams will be dparams if statement before is true, or $DailyParams->avg($dparams1, $dparams2) if it is not. Second is the same thing but in a more straight forward fashion.

1 Comment

Thank you for putting it into code form for myself - I have been reading up on the ternary operator but couldn't quite grasp it until this. Thanks so much!
0

This is called the ternary operator. It sets a variable based on a condition. In PHP, what it's saying (in pseudocode here) is:

setSomeVariable = (test condition) ? value if true : value if false;

So, if for example, you wanted to set the value of $a to 10 if the $score is greater than 100, or set $a to 0 if $score is less than or equal to 100, the code would look like this:

$a = ($score > 100) ? 10 : 0;

See the documentation here: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

1 Comment

@Pointy I removed that statement. Thank you.
0

It's called a ternary operator; it's used the same way in PHP as it is in JavaScript -

/* most basic usage */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true

Straight from David Walsh's blog - http://davidwalsh.name/php-shorthand-if-else-ternary-operators

Also note: I think you're thinking about it a bit backwards -

It is not (repeat, not) used like this:

$var = (assign this value) ? (if this is true) : (otherwise, assign this value)

It is used like this:

$var = (is this true) ? (then assign this value) : (otherwise, assign this value)


EDIT Per @Pointy pointy-ing this out, there is a slight difference between how the statements are evaluated in JavaScript in PHP - See example #4 in the documentation. Although only a crazy person would write a long-winded unparenthesized ternary expression. http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

7 Comments

No, it's not the same in PHP.
If the answer is wrong then explain how/why please.
PHP has a right-hand bias to interpreting a sequence of ? : operations, while JavaScript (and pretty much every other language that has the construct) is left-biased.
@Pointy So they're the same unless you nest them without parenthesizing?
Nested ternaries are horrible, doing them without parentheses doubly so.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.