Optimize ternary operator

That’s just horrible code. It’s badly formatted. I don’t see the hierarchy of the expression. Even if it had good formatting, the expression would be way too complex to quickly parse with the human eye. The intention is unclear. What’s the purpose of those conditions? So what can you do? Use conditional statements (if). Extract … Read more

Javascript && operator versus nested if statements: what is faster?

The performance difference is negligible. The && operator won’t check the right hand expression when the left hand expression evaluates false. However, the & operator will check both regardless, maybe your confusion is caused by this fact. In this particular example, I’d just choose the one using &&, since that’s better readable.

How do I use the conditional (ternary) operator?

It works like this: (condition) ? true-clause : false-clause It’s most commonly used in assignment operations, although it has other uses as well. The ternary operator ? is a way of shortening an if-else clause, and is also called an immediate-if statement in other languages (IIf(condition,true-clause,false-clause) in VB, for example). For example: bool Three = … Read more

Benefits of ternary operator vs. if statement

Performance The ternary operator shouldn’t differ in performance from a well-written equivalent if/else statement… they may well resolve to the same representation in the Abstract Syntax Tree, undergo the same optimisations etc.. Things you can only do with ? : If you’re initialising a constant or reference, or working out which value to use inside … Read more

PHP short-ternary (“Elvis”) operator vs null coalescing operator

Elvis ?: returns the first argument if it contains a “true-ish” value (see which values are considered loosely equal to true in the first line of the Loose comparisons with == table). Or the second argument otherwise $result = $var ?: ‘default’; // is a shorthand for $result = $var ? $var : ‘default’; Null … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)