Home » Php » Using Ternary Operator without the Else statement PHP

Using Ternary Operator without the Else statement PHP

Posted by: admin April 23, 2020 Leave a comment


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);

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?

How to&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.


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.


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.


Just set it to NULL like this:

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

The you variable would return false with a isset() check.

You can read more about NULL in the manual.

And a quote from there:

The special NULL value represents a variable with no value. NULL is the only possible value of type null.

A variable is considered to be null if:

  • it has been assigned the constant NULL.
  • it has not been set to any value yet.
  • it has been unset().


You can also do this (short form):

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