Now that we’ve covered the basics of what functions are within PHP – PHP has a wide array of functions already available for you to use to work with various strings. So let’s say that we have a variable holding a user’s telephone number and we’re asking our user to input the last 4 digits of their phone number to verify themselves. To do this we’re going to use the help of the substr (substring) function.

$phone = "212-123-4567";
$phone_lastFour = substr($phone, 8, 4);

The PHP substring function takes in 3 total arguments. First off is the string you want to get the substring from, $phone in our above example. Next is the index of the first character to be extracted – or essentially the count of where the first character to be utilized is inside the string. In our example since the phone number format has a total of 12 characters and we only want the last 4 – our first character index is 8. Finally comes the length parameter which specifies the length of the substring you wish to extract. This is the only optional parameter within the substr function as if you leave the length argument out then the returned substring will go all the way to the end of the string. For the purpose of this example we will specify that we want only 4 characters – however we can technically leave this blank as-well.

Now let’s make use of our previous php tutorials covering variable scope, and if/else statements to write the remainder of our script:


	Something doesn\'t seem right. Please try again. 
'; } } else { echo '
'; } ?>


So the first thing we are doing is creating a simple if/else statement to check if the $POST supervariable is set. If it is set then it’s safe to assume the form has been submitted as it is sending data via the POST method. So we take our submitted information and store that into a variable called $last4Verify. Now we simply create an additional if/else statement to check to see if the information that our visitor has entered matches that which we have. If it doesn’t we produce an error message and the form again; alas if it does we give them a success message.

However there is a slight issue we have left. A phone number can be written in many different ways.

  • 212-123-4567
  • 1212-123-4567
  • 1-212-123-4567
  • 2121234567

In all of the above examples the last 4 digits remain the same ofcourse, however the length of the string changes and as such specifying an index for the first of the 4 characters is impossible. Luckily the substr function lets you count from the back.

$phone = "2121234567";
$phone_lastFour = substr($phone, -4, 4);

Here we’re telling PHP we want to get the substring starting from the 4th character from the end. However there is an alternative way also. We can use yet another string function of PHP called strlen which finds the length of a certain string. We can then always find our starting index by subtracting 4 from the length of our string.

$phone = "2121234567";
$length = strlen($phone);
$lastfourLength = $length - 4;
$phone_lastFour = substr($phone, $lastfourLength, 4);

Published by Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Leave a comment

Your email address will not be published. Required fields are marked *