Hướng dẫn php ternary without else

Can you use the Ternary Operator in PHP without the closing 'else' statement? I've tried it and it's returning errors. Google search isn't yielding anything, so I think the answer is probably no. I just wanted to double check here. For instance:

if ( isset($testing) {
  $new_variable = $testing;
}

Will only set $new_variable if $testing exists. Now I can do

$new_variable = (isset($testing) ? $testing : "");

but that returns an empty variable for $new_variable if $testing isn't set. I don't want an empty variable if it's not set, I want the $new_variable to not be created.

I tried

$new_variable = (isset($testing) ? $testing);

and it returned errors. I also tried

$new_variable = (isset($testing) ? $testing : );

and it also returned errors. Is there a way to use the Ternary Operator without the attached else statement, or am I stuck writing it out longhand?

EDIT: Following Rizier123's advice, I tried setting the 'else' part of the equation to NULL, but it still ends up appending a key to an array. The value isn't there, but the key is, which messes up my plans. Please allow me to explain further.

The code is going to take a bunch of $_POST variables from a form and use them for parameters in a stdClass which is then used for API method calls. Some of form variables will not exist, as they all get applied to the same variable for the API call, but the user can only select one. As an example, maybe you can select 3 items, whichever item you select gets passed to the stdClass and the other 2 don't exist.

I tried this:

$yes_this_test = "IDK";
$setforsure = "for sure";
$list = new stdClass;
$list->DefinitelySet = $setforsure;
$list->MaybeSet = (isset($yes_this_test) ? $yes_this_test : NULL);
$list->MaybeSet = (isset($testing) ? $testing : NULL);
print_r($list);

But obviously MaybeSet gets set to NULL because (isset($testing) comes after (isset($yes_this_test) and it returns

stdClass Object ( [DefinitelySet] => for sure [MaybeSet] => )

I won't know what order the $_POST variables are coming in, so I can't really structure it in such a way to make sure the list gets processed in the correct order.

Now I know I can do something like

if ( isset($yes_this_test ) {
  $list->MaybeSet = $yes_this_test;
}
elseif ( isset($testing) ) {
  $list->MaybeSet = $testing;
}

But I was hoping there was a shorthand for this type of logic, as I have to write dozens of these. Is there an operator similar to the Ternary Operator used for if/elseif statements?

Để lập trình nhanh hơn, trong ngôn ngữ lập trình PHP, chúng ta nên thường xuyên sử dụng các phép toán Phép toán Ternary và Ternary Coalescing. Có những phép toán nào thì hãy tham khảo bài viết sau đây của vinasupport.com nhé!

Nội dung chính

  • 1. Phép toàn Ternary: cond ? expr1 : expr2
  • 2. Phép toán Ternary: cond ?: else-expr
  • 3. Phép toán Null Coalescing
  • 4. Phép toán Null Coalescing Assignment

Nội dung chính

  • 1. Phép toàn Ternary: cond ? expr1 : expr2
  • 2. Phép toán Ternary: cond ?: else-expr
  • 3. Phép toán Null Coalescing
  • 4. Phép toán Null Coalescing Assignment

1. Phép toàn Ternary: cond ? expr1 : expr2

Phép toán thường được sử dụng nhiều nhất.

$rank = $marks >= 50 ? 'pass' : 'fail';

Nó có nghĩa là:

if ($marks >= 50) { $rank = 'pass'; } else { $rank = 'fail'; }

2. Phép toán Ternary: cond ?: else-expr

$user = load_user() ?: false;

Nó tương đương với

$user = load_user() ? load_user() : false;

3. Phép toán Null Coalescing

$result = $_GET['value'] ?? 'foo';

Nó tương đương với

$result = isset($_GET['value']) ? $_GET['value'] : 'foo';

Thích hợp cho set default các giá trị parameter của url

4. Phép toán Null Coalescing Assignment

$value ??= 'foo';

Nó tương đương với

$value = $value ?? 'foo';

Để sử dụng các phép toán trên, các bạn phải sử dụng php version 7.4 trở lên.

I want to write the following code in ternary operator. I tried in many way but it does not work at all.

<?php 

if(isset($options['footer_txt_color'])) {

    echo $options['footer_txt_color'];

} else {

    echo "#ffffff";

}

?>

asked Feb 19, 2015 at 9:11

1

Use this code

echo (isset($options['footer_txt_color'])) ? $options['footer_txt_color'] : '#ffffff';

answered Feb 19, 2015 at 9:15

Sunil PachlangiaSunil Pachlangia

1,9832 gold badges13 silver badges25 bronze badges

It should be like this:

<?php echo (isset($options['footer_txt_color'])) ? $options['footer_txt_color'] : "#ffffff"; ?>

answered Feb 19, 2015 at 9:13

AlbziAlbzi

15.2k6 gold badges44 silver badges61 bronze badges

Not the answer you're looking for? Browse other questions tagged php operator-keyword ternary or ask your own question.