0

I have two variables, $firstname & $lastname

I want to combine them as another variable $realname

So example: if firstname=John &lastname=Smith, then I want realname=John Smith

Would this be the correct usage?

$realname = $firstname & " " & $lastname;
?>
  • Just use dot instead of `&` like this: `$realname = $firstname . " " . $lastname;`. – Amirreza Noori Mar 26 '22 at 03:57
  • Does this answer your question? [How can I combine two strings together in PHP?](https://stackoverflow.com/questions/8336858/how-can-i-combine-two-strings-together-in-php) – Adesh Kumar Mar 28 '22 at 04:47
  • On the php manual : https://www.php.net/manual/en/language.operators.string.php – LBS Mar 29 '22 at 10:47

1 Answers1

1
<?php   
    $realname = "$firstname $lastname";
?>

Concatenation in php is achieved by concat operator i.e .

Sherif
  • 11,786
  • 3
  • 32
  • 57
Shoyeb Sheikh
  • 2,659
  • 2
  • 10
  • 19
  • OK, Now I'd like to take this one step further and auto add a unique UserName based on the first/lastname... BUT, I need to check the DB for duplicates and append a consecutive number at the end until it is unique. (Example: The second John Smith who registers shoud be John_Smith2, the third John_Smith3, etc.) $username = $firstname . "_" . $lastname; Any ideas? – Roger Rosentreter Mar 26 '22 at 22:51
  • Sorry that should have read John_Smith1 & John_Smith2 for the second and third ones... – Roger Rosentreter Mar 26 '22 at 23:04