How to Get Last Character from String in PHP?

By Hardik Savani May 14, 2024 Category : PHP

Hey,

In this guide, we are going to learn php get last character from string. you will learn how to get last character from string in php. We will look at an example of php get last character from string example. you will learn get last character from string php.

we will use substr() php function to get last character from string. i will give you very simple examples to get last character from string in php.

Example 1: PHP Get Last Character from String

index.php

<?php

/* Declare string variable */

$myString = "Welcome to ItSolutionStuff.com!";

/* get Last Character of string */

$char = substr($myStr, -1);

/* Echo resulted string */

echo $char;

?>

Output:

!

Example 2: PHP Get First Character from String

index.php

<?php

/* Declare string variable */

$myString = "Welcome to ItSolutionStuff.com!";

/* get First Character of string */

$char = substr($myString, 0, 1);

/* Echo resulted string */

echo $char;

?>

Output:

W

Example 3: PHP Get First and Last Character from String

index.php

<?php

/* Declare string variable */

$myString = "Welcome to ItSolutionStuff.com!";

/* get First and Last Character of string */

$last = substr($myString, 0, 1);

$last = substr($myString, -1);

/* Echo resulted string */

echo $last;

echo $last;

?>

Output:

W

!

I hope it can help you...

Tags :
Shares