Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Ternary Operator without the Else statement PHP

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?

like image 665
Longblog Avatar asked Mar 31 '15 17:03

Longblog


People also ask

Can we use if without else in PHP?

An if statement by its self is absolutely fine. If you have a series of conditions where only one will be true it's better to put the most likely first, then use else if on the subsequent ones so that PHP doesn't have to keep evaluating each condition.

Do ternary operators need an else?

A ternary operation is called ternary because it takes 3 arguments, if it takes 2 it is a binary operation. It's an expression returning a value. If you omit the else you would have an undefined situation where the expression would not return a value. You can use an if statement.

How use ternary operator in if condition in PHP?

It is called a ternary operator because it takes three operands- a condition, a result statement for true, and a result statement for false. The syntax for the ternary operator is as follows. Syntax: (Condition) ? (Statement1) : (Statement2);

Does PHP have a ternary operator?

The ternary operator is the only operator in PHP which requires three operands: the condition, the true and the false result.


1 Answers

Since PHP 5.3 you can do this:

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

As you can see, it only uses the part if the condition is false, so you have to negate the isset expression.

UPDATE

Since PHP 7.0 you can do this:

$new_variable = $testing ?? null;

As you can see, it returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

UPDATE

Since PHP 7.4 you can do this:

$new_variable ??= $testing;

It leaves $new_variable alone if it isset and assigns $testing to it otherwise.

like image 145
Daan Avatar answered Oct 03 '22 02:10

Daan