Here, I will demonstrate how to retrieve the final two characters of a string using PHP. We will utilize PHP to get the string’s final four characters. Learn how to obtain the final three characters of a string in PHP. If you have a query regarding how to use PHP to retrieve the final five characters of a string, I will provide a brief example and a solution.
In this PHP example, we will utilize the substr() function to retrieve the string’s final 2, 3, 4, 5, and 7 characters. Consequently, let us examine the following example code:
<?php
$string = "hii this is abhishek";
$last2Char = substr($string, -2);
var_dump($last2Char);
$last3Char = substr($string, -3);
var_dump($last3Char);
$last4Char = substr($string, -4);
var_dump($last4Char);
$last5Char = substr($string, -5);
var_dump($last5Char);
$last7Char = substr($string, -7);
var_dump($last7Char);
?>
// Output
string(2) "ek"
string(3) "hek"
string(4) "shek"
string(5) "ishek"
string(7) "hishek"

I’m Abhishek, a DevOps, SRE, DevSecOps, and Cloud expert with a passion for sharing knowledge and real-world experiences. I’ve had the opportunity to work with Cotocus and continue to contribute to multiple platforms where I share insights across different domains:
-
DevOps School – Tech blogs and tutorials
-
Holiday Landmark – Travel stories and guides
-
Stocks Mantra – Stock market strategies and tips
-
My Medic Plus – Health and fitness guidance
-
TrueReviewNow – Honest product reviews
-
Wizbrand – SEO and digital tools for businesses
I’m also exploring the fascinating world of Quantum Computing.