PHP program to find the length of the last word in string Last Updated : 24 May, 2022 Comments Improve Suggest changes Like Article Like Report We are given a string. We are required to write a program in PHP to find the length of the last word in the string using inbuilt functions. We have already discussed approach of solving this problem here. This article discusses the PHP solution to the problem. Examples: Input : "php exercises" Output : 9 Input : "geeks for geeks" Output : 5 We will mainly use these three inbuilt function in PHP to solve this problem: substr() Function: This inbuilt function in PHP is used to extract a part of string.strrpos() Function: This inbuilt function in PHP is used to find the last position of string in original or in another string. It returns the integer value corresponding to position of last occurrence of the string, also it treats uppercase and lowercase characters uniquely.strlen() Function: This inbuilt function in PHP is used to find the length of a string. The idea to solve this problem using the above mentioned inbuilt function is to first find the position of last occurring space in the string using the strrpos() function. After getting the position of last occurring space we can easily get the last word in the string using the substr() function and store this in a new string variable. At last, we can use the strlen() function to find the length of the last word in the string. PHP <?php // PHP code to find the length of last word // in a string // function to find length of last word function length_last_word($string) { // position of last occurring space // in the string $pos = strrpos($string, ' '); // if the string has only one word if(!$pos) { $pos = 0; } else { $pos = $pos + 1; } // get the last word in the string $lastWord = substr($string,$pos); // return length of last word return strlen($lastWord); } // Driver Code print_r(length_last_word('geeksforgeeks')."\n"); print_r(length_last_word('computer science portal')."\n"); ?> Output: 13 6 Create Quiz Comment A akash1295 Follow 0 Improve A akash1295 Follow 0 Improve Article Tags : Misc Web Technologies PHP PHP-function Explore BasicsPHP Syntax4 min readPHP Variables5 min readPHP | Functions6 min readPHP Loops4 min readArrayPHP Arrays5 min readPHP Associative Arrays4 min readMultidimensional arrays in PHP5 min readSorting Arrays in PHP4 min readOOPs & InterfacesPHP Classes2 min readPHP | Constructors and Destructors5 min readPHP Access Modifiers4 min readMultiple Inheritance in PHP4 min readMySQL DatabasePHP | MySQL Database Introduction4 min readPHP Database connection2 min readPHP | MySQL ( Creating Database )3 min readPHP | MySQL ( Creating Table )3 min readPHP AdvancePHP Superglobals6 min readPHP | Regular Expressions12 min readPHP Form Handling4 min readPHP File Handling4 min readPHP | Uploading File3 min readPHP Cookies9 min readPHP | Sessions7 min read Like