Using Ternary Operator without the Else statement PHP -


can use ternary operator in php without closing 'else' statement? i've tried , it's returning errors. google search isn't yielding anything, think answer no. wanted double check here. instance:

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

will set $new_variable if $testing exists. can

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

but returns empty variable $new_variable if $testing isn't set. don't want empty variable if it's not set, want $new_variable not created.

i tried

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

and returned errors. tried

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

and returned errors. there way use ternary operator without attached else statement, or stuck writing out longhand?

edit: following rizier123's advice, tried setting 'else' part of equation null, still ends appending key array. value isn't there, key is, messes plans. please allow me explain further.

the code going take bunch of $_post variables form , use them parameters in stdclass used api method calls. of form variables not exist, applied same variable api call, user can select one. example, maybe can select 3 items, whichever item select gets passed stdclass , 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 maybeset gets set null because (isset($testing) comes after (isset($yes_this_test) , returns

stdclass object ( [definitelyset] => sure [maybeset] => ) 

i won't know order $_post variables coming in, can't structure in such way make sure list gets processed in correct order.

now know can like

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

but hoping there shorthand type of logic, have write dozens of these. there operator similar ternary operator used if/elseif statements?

since php 5.3 can this:

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

as can see, uses part if condition false, have negate isset expression.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -