What is substr() in php?

substr()  This function returns a part of a string.

substr(string,start,length)

string :   The input string. Must be one character or longer.

start :     The start is how many characters in to skip before starting. Setting this to 3 would skip the first three and start returning at the forth character. Setting this to a
negative number  will start counting backwards from the end of the string.

length:  Length is an optional parameter. If you set this to a positive number, it will return that number of characters. If you set this to a negative number it will count that many
numbers from the end of the string and return whatever is left in the middle.

<?php
echo substr(‘abcdef’, 1);     // bcdef
echo substr(‘abcdef’, 1, 3);  // bcd
echo substr(‘abcdef’, 0, 4);  // abcd
echo substr(‘abcdef’, 0, 8);  // abcdef
echo substr(‘abcdef’, -1, 1); // f

// Accessing single characters in a string
// can also be achieved using “square brackets”
$string = ‘abcdef’;
echo $string[0];                 // a
echo $string[3];                 // d
echo $string[strlen($string)-1]; // f

?>

 

//