• Post category:PHP

Substring Function in PHP | Explained

Substring Function in PHP

Learn the substring function in php with example codes. The substr() indicates the substring function and it is used to retrieve a section of the string. It is one of the PHP in-built function. PHP language is abundant with built-in functions that operate on both strings and numbers.


substr(string, start, length);

When we use the substr() function, the first character of the string is treated as offset 0, the second character as offset 1, and so on. We can pass three arguments in this function, first, we pass the “string or string assigned variable” then we pass the “starting position” and then we pass “a number of characters to extract starting from the specified offset”. However, PHP also supports shortcut syntax to accomplish the same thing. This consists of specifying the offset of the character to be retrieved within curly braces, after the variable name.

Example :

<!DOCTYPE html>

// passing string directly
echo substr("Welcome to nowhere", 11, 3);
echo "<br/>";

// passing string via variable
$var = "Welcome to nowhere";
echo substr($var, 3,4);



substring function in php

Leave a Reply