How do you print a variable in PHP that's either one element of an array, or the whole variable if it's not an array -




say have $examplevariable, want print. $examplevariable may array, in case have set right array element, can print print $examplevariable[$i].

if ($_get) {     $i = array_search($_get["examplequerystring"], $examplevariable); } elseif (is_array($examplevariable)) {     $i = 0; } else {     $i = ""; } 

my problem last else, if $examplevariable not array, because print $examplevariable[] doesn't work. there can put $i print whole variable?

alternatively, considered including brackets in $i, i'd have example $i = [0];, in case don't know how i'd print it. $examplevariable$i won't work.

i have number of variables besides $examplevariable i'll need print, same $i or lack thereof, i'd not have longwinded set each of them individually.

this sounds way more complicated feel should, makes sense!

you can nifty thing called type casting. means, can make variable array if not, prepending name (array):

$examplevariable = (array)$examplevariable; 

so don't need 3 if branches @ all:

if ($_get) {      $i = array_search($_get["examplequerystring"], $examplevariable); } else {     $i = 0;     $examplevariable = (array)$examplevariable; } 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -