In this blog, we will learn how to get the first and last characters from a string in PHP. This is going to be very easy and simple.
We will be going to use substr() function of PHP to get the first and last characters. I will be explain with examples.
Let's start with examples,
PHP Get First and Last Character from String
Code:
<?php
/* Declare string variable */
$myString = "This is thecodingdev.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:
T
!
I hope you understood this. If you have any doubt let me know in comments.
0 Comments