Asked  7 Months ago    Answers:  5   Viewed   35 times

Possible Duplicate:
Reference - What does this symbol mean in PHP?

I've been doing conditionals with if/else or a year or so now. Looking at some new code, I'm seeing a conditional that appears to use ? and : instead of if and else. I'd like to learn more about this, but I am not sure what to google to find articles explaining how it works. How can I do it?



It's the Ternary Operator.

Basic usage is something like

$foo = (if this expressions returns true) ? (assign this value to $foo) : (otherwise, assign this value to $foo)

It can be used for more than assignment though, it looks like other examples are cropping up below.

I think the reason you see this in a lot of modern, OO style PHP is that without static typing you end up needing to be paranoid about the types in any particular variable, and a one line ternary is less cluttered than a 7 line if/else conditional.

Also, in deference to the comments and truth in naming, read all about the ternary operators in computer science.

Wednesday, March 31, 2021
answered 7 Months ago

Yes, you can. From the PHP Doc:

[...] the ternary operator is an expression, and [...] it doesn't evaluate to a variable, but to the result of an expression.

That quoted, although the ternary is mostly used for assigning values, you can use it as you suggested because a function call is an expression so the appropriate function will be evaluated (and therefore executed).

If your setter function would return a value, it would simply not be assigned to anything and would therefore be lost. However, since you said your setter doesn't return anything, the whole ternary operator will evaluate to nothing and return nothing. That's fine.

If this is more readable than a regular if/else is a different story. When working in a team, I would suggest to rather stick to a regular if/else.

Or, how about going with this syntax, which would be more common?

$this->myVar = isset($someVar) ? 'something' : 'something else';

Or, using your setter:

$this->setMyVar(isset($someVar) ? 'something' : 'something else');
Saturday, May 29, 2021
answered 5 Months ago

This is the conditional operator.

$x ? $y : $z

means "if $x is true, then use $y; otherwise use $z".

It also has a short form.

$x ?: $z

means "if $x is true, then use $x; otherwise use $z".

People will tell you that ?: is "the ternary operator". This is wrong. ?: is a ternary operator, which means that it has three operands. People wind up thinking its name is "the ternary operator" because it's often the only ternary operator a given language has.

Tuesday, June 1, 2021
answered 5 Months ago

At the bottom panel you should have "5: Debug". Click on it and select "Debugger -> Threads"

You may need to find the "Threads" icon on the far right, or even click the "Restore Layout" button on the left to restore this window.

enter image description here

Sunday, August 1, 2021
answered 3 Months ago

On the off chance that you know German (yes, sorry …), a friend and I have written an introduction with code about this subject which I myself find quite passable. The text and code uses the example of TSP to introduce the concept.

Even if you don't know German, take a look at the code and the formulas in the text, this might still serve.

Sunday, September 12, 2021
Aidan D
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :