December 29, 2025|PHP|

PHP How to get first and last name from full name?

Sometimes you may need to split a full name into separate parts, such as a first name and last name, for display or processing purposes. PHP provides helpful string and array functions that make this task easy and efficient.

In this example, we use the explode() function to break a full name into an array based on spaces, safely access the first name using the null coalescing operator (??), and retrieve the last name using the end() function. This approach is commonly used when handling user input in PHP applications.

<?php
$c_name = explode(' ', "Gaurav R Joshi");
echo   $firstName = $c_name[0] ?? '';
echo $lastName = end($c_name);

 

- END -


Leave A Comment

Get fresh content from StatelyWorld