Home > Software design >  if(isset) echo something, else echo something else, short isset, php
if(isset) echo something, else echo something else, short isset, php

Time:06-18

i haven't got mucho experience and I saw this post that I tought it was interesting about shorthand for isset()

where someone asks about shorting if(isset) and the answer given was shomething like:

$var = isset($var) ? $var : "default";

or

isset($var) ?: $var = 'default';

an I was curious and the solutions given work great for one variable at the time, so I've been trying to do the same but with two variables at the same time.

So i wanna check $firstname and $lastname, if they are there then echo, else just print plain text 'noNames'

 $fullname = isset($firstname, $lastname) ? ($firstname && $lastname) : 'noNames'; echo $fullname;

I just wanted to check them so when I'm offline editing don't get that error warning in line bla bla bla, for not setting the variable.

So how can this be done? in simple way, I don't wanna add to much for something that is more curiosity than anything else. Thanks.

CodePudding user response:

Something like this to check if multiple variables are set. Concatenate the first and last to put in the fullname or put 'noNames'.

$fullname = (isset($firstname, $lastname)) ? ($firstname.' '.$lastname) : 'noNames'; 
echo $fullname;

CodePudding user response:

and also this:

<?php

    //$firstname='Pinky';$lastname='Miss';
//  ^-----uncomment
(isset($firstname, $lastname) and print($firstname.' '.$lastname)  ) or print 'noNames';

?>
  •  Tags:  
  • php
  • Related