is possible multiple value in php variable with condition (or). such as like this
$shippingCost = $selectedShipping['cost'] || null;
I've tried but it gives return error
CodePudding user response:
We use ternary operators to assign values to parameter when there are conditions to check. otherwise you can simply use if else conditions as well
using ternary operator
$shippingCost = isset($selectedShipping['cost']) ? $selectedShipping['cost'] : null
using if,else
$shippingCost = 0;
if(isset($selectedShipping['cost'])){
$shippingCost = $selectedShipping['cost'];
}
CodePudding user response:
if I recall correctly, ternary operator (?:) tries to find if the value is truthy or falsy, so if it is not set, it will return an error or a warning.
What you are looking for, is passing a value depending on if its set or not.
Back in old times we've had isset($array['key'])
or array_key_exists
for the senior guys, however right now what you can do is use a null coalescing operator - ??.
$array['key'] ?? null
- this will return whatever is under that key in the array, or null if its undefined.