How to replace if statement with a ternary operator ( ? : )?
The
(condition) ? /* value to return if condition is true */
: /* value to return if condition is false */ ;
syntax is not a "shorthand if" operator (the ?
is called the conditional operator) because you cannot execute code in the same manner as if you did:
if (condition) {
/* condition is true, do something like echo */
}
else {
/* condition is false, do something else */
}
In your example, you are executing the echo
statement when the $address
is not empty. You can't do this the same way with the conditional operator. What you can do however, is echo
the result of the conditional operator:
echo empty($address['street2']) ? "Street2 is empty!" : $address['street2'];
and this will display "Street is empty!" if it is empty, otherwise it will display the street2 address.
How can I change the if else statement in the function with Ternary operator in JavaScript?
The code is very inefficient because you find the thing, and then you turn around and find the thing again. So you end up looping multiple times.
To make it more readable, I broke it up into parts. It also loops one time over the object to locate the items with children and the items without children. There is a ternary operator there to handle with and without.
The code then determines if it is the child or not and grabs the object.
// Grab the package
var selectedPackageProducts = this.deal.packages.find(p => p.isSelected).dealProducts;
// check to see if the children has the product type or if the parent does (if no children)
const selectedProduct = selectedPackageProducts.find(dp =>
dp.children.length > 0 ?
dp.children[0].product.productTypeCode === productType :
dp.product.productTypeCode === productType)
// If we have children use it, else reference the parent
const productObj = selectedProduct && selectedProduct.children.length ?
selectedProduct.children[0] :
selectedProduct;
// get the product name
const productName = productObj && productObj.product.name
replace '.' with '' in ternary operator
var arr = '.net'
arr = arr.startsWith('.')
? arr.slice(1)
: arr
You probably don't want to replace the .
as there could be a .
elsewhere in the string. .slice
will just remove the first character and return the rest.
Why can't I replace if statement with conditional operator (?:)?
The syntax is:
condition ? value1 : value2;
not
condition ? statement1 : statement2;
The conditional operator is an expression, not a statement. It doesn't execute statements like an if
statement does: it returns a value.
What you mean is:
preferredClass = (preferredClass == PlaneClass.FIRST_CLASS ?
PlaneClass.ECONOMY_CLASS : PlaneClass.FIRST_CLASS);
Change from if else to ternary operator
It's not generally a good idea to use nested conditional operators, but it can be done:
printf("%s", a==b ? "equal" : (a > b ? "bigger" : "smaller"));
Related Topics
When Should I Use MySQLi Instead of MySQL
Remove Non-Utf8 Characters from String
Suppress Error With @ Operator in PHP
Is There a Function to Extract a 'Column' from an Array in PHP
Ajax Request Callback Using Jquery
Multiple Inputs With Same Name Through Post in PHP
PHP - Concatenate or Directly Insert Variables in String
Session Variables Not Working PHP
Compiling an Ast Back to Source Code
"Fatal Error: Cannot Redeclare ≪Function≫"
Convert a Series of Parent-Child Relationships into a Hierarchical Tree
How to Generate in PHP All Combinations of Items in Multiple Arrays