echo session first name and last initial and full name

2.9k Views Asked by At
$_SESSION['first']=$result['first_name'];
$_SESSION['last']=$result['last_name'];

Need to echo out first name and last name. And First name and last initial. that should look like:

Joe S. Joe Smith

<option value="Realname"><?php echo $_SESSION['first'] ?></option>

I know this one only gets the first name. Not sure how to from here.

Any suggestions?

3

There are 3 best solutions below

0
On
<option value="Realname"><?php echo $_SESSION['first'].$_SESSION['last'] ?></option>
0
On

Use the substr function to get only the first character of the last name.

<option value="Realname"><?php echo $_SESSION['first'] . ' ' 
    . substr($_SESSION['last'], 0, 1) . '.' ?></option>

To show the full last name, simply concatenate the full variable.

<option value="Realname"><?php echo $_SESSION['first'] . ' ' 
    . $_SESSION['last'] ?></option>
0
On

1) Joe S.

Strings can be seen as Char Arrays. To get first character, web can use [0] (I'm pretty sure is much faster than the substr method).

<option value="Realname"><?php echo $_SESSION['first'] . ' ' . $_SESSION['last'][0] . '.'  ?></option>

2) Joe Smith

Use just concatenate operator.

<option value="Realname"><?php echo $_SESSION['first'] . ' ' . $_SESSION['last']  ?></option>