PHP Ternary Operator Clarification

PHP Ternary operator clarification

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

Source

For example

$used_value = function1() ?: $default_value;

Is the same as

$check_value = function1(); //doesn't re-evaluate function1()
if( $check_value ) {
$used_value = $check_value;
} else {
$used_value = $default_value;
}

Word for the wise

If your going to be depending on typecasting to TRUE it's important to understand what WILL typecast to TRUE and what won't. It's probably worth brushing up on PHP's type juggling and reading the type conversion tables. For example (bool)array() is FALSE.

Do you have to include a false value in a ternary operator PHP?

You can't skip it. You could re-write as a one line if condition though:

if (condition()) $string = 'something'.$string.'something';

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.

PHP, Shorthand, If..Else using Ternary Operators

Its supposed to be:

(conditions) ? true : false
satisfies <--^ ^----> did not satisfy

So this equates into:

$F_NAME = isset($_POST['F_NAME']) ? $_POST['F_NAME'] : $_SESSION['USR']['F_NAME'];

Ternary Operators. Possible for a one sided action?

Short-circuit

isset($foo) and callFunction();

Reverse the condition and omit the second argument

!isset($foo) ?: callFunction();

or return just "something"

isset($foo) ? callFunction() : null;

However, the ternary operators is designed to conditionally fetch a value out of two possible values. You are calling a function, thus it seems you are really looking for if and misuse ?: to save characters?

if (isset($foo)) callFunction();

PHP - ternary for undefined index

Yes, PHP has ternary operator.

Like this:

$variable = ( condition ? true : false );

So in your case it like this:

$chains[$key] = ( !empty($chain_product['inventory_amount']) ? intval($chain_product['inventory_amount']) : 0 );

so if you have inventory_amount empty, it will have 0 assigned as a "fallback".

PHP Ternary Operator (?) Usage For Functions?

The arguments to the ternary operator, like any other operator, must be expressions. require_once is PHP syntax, it's not a function, so it can't be used within an expression. So you should use the ternary operator in the argument to require_once, which is an expression.

require_once (getStatus() ? 'a.php' : 'b.php');

For your extra request, you can do it with the ternary operator, but the result is not pretty, and IMHO harder to understand than the original:

call_user_func_array('setcookie', 
websiteIsLive() ?
array("token", x, 0, '/', $_SERVER['HTTP_HOST'], true) :
array("token", y, 0, '/'));


Related Topics



Leave a reply



Submit