Both the function extract the part of the string from the given string. So I will concentrate on difference rather than full detailed explanation of these functions.
Both the functions have same number of parameters (two), but the parameter which makes them behave different than each other is the second parameter. Have a look at more details on this.
First Parameter indicates the starting point and second parameter indicates length of the string you want to generate.
So if you want to get the substring which start from second character and have a length of 5 character the below code with be used.
Like substr first parameter in substring indicates the starting index but second parameter indicates the ending index of the string.
So if you want to get the string from main string which start from second character and end at fifth char, below code will be used.
Now lets apply same parameter on both functions and check the output from the same string.
Hope you are clear with difference in these two functions. So you would have better idea for which one to use.
Share your thoughts on this via comments.
Avinash Zala currently working at a leading web development company as a Sr. PHP Web Developer. He is the founder of the Xpert Developer. He has completed B.E. in Information Technology. Born and brought up in Ahmedabad. He loves to code and coding is all about passion for him. Follow him on twitter at Avinash Zala & Xpert Developer.
View all posts by: Avinash