Up Next
Up Next

PHP Delete First Character in String

Ryan Dube
PHP Code
What Does PHP Stand For?

One of the most common questions PHP programmers have when manipulating string variables is what command does a PHP delete first character in string variables. There are a lot of different ways to accomplish this task, but there's really only one PHP command that does the trick quickly and easily.

Doing a PHP Delete First Character in String Variables

It is a common occurrence when you are processing user input from a web page or otherwise manipulating string variables that you need to use PHP to delete the first character in string variables. The simple answer is that you should use the "substr" command, but before you immediately jump into throwing the substr command into your script, it's important to understand exactly how this function does a PHP delete first character in string variables.

How the SUBSTR Function Works in PHP

In PHP, just as in many other programming languages, the substr() function simply takes a string and returns part of it to another variable however you tell it to. This means that this function will does not only return the first character in a string, but you could also use it to return a certain number of middle characters, the first few characters, or you could even use it to return a certain number of characters after the position of a particular character or symbol. It is a very useful and versatile function that PHP programmers use often. The function works as follows.

  • The syntax of the substr() function is "substr(string1,start,string_length)"
  • In the "start" position of the function, you define the position where the function should start extracting characters. Position 0 is the very first character.
  • In the "length" position of the function, you define the number of characters you want the function to extract. So, if you would like to get five characters after the starting point, this would be "5." This parameter is optional, and if you leave it blank the function will simply extract the rest of the string.

A Simple Working Example

There are a number of ways that you can use the substr function to delete the first character of a string, but the most common and simplest method is show here.

<?php $string2 = substr($string1,1); ?>

This is the simplest form of this function. Your original string is $string1 (strings are always preceded by a dollar sign). The substr function then starts at position 1 (not 0), and extracts the rest of the string. This essentially removes the very first character from the string and places the new string into the string variable $string2. So, a string like "three little pigs" would become "hree little pigs".

Creating a Common Subroutine

If you're creating a PHP script that needs to make use of this feature often, you wouldn't want to keep repeating the same substr() command over and over. Instead, you can simplify your program by creating a new function within your script that can handle processing the task of removing the first character from a string. This is very useful for when you have to remove the first number from a phone number or when you need to remove a special character symbol from a URL parameter. You can create such a function in your PHP script as follows.

<?php function removeFirstChr($string2){ return substr($string2,1); } ?>

Now that you have a function that will immediately return the entire string with the first character removed, all you have to do is pass any string to the function and it will perform the processing for you. This allows you to use the following simple command everywhere in your program where you need this feature.

<?php $processedString = removeFirstChr($string1); ?>

It's that simple - just pass the original string to your new function and you'll receive the entire string without the first character into your new defined string variable.

Final Words

As you can see in the script examples above, deleting the first character from string variables isn't difficult at all. As with most PHP functions, once you know the right function to use and how it works, using it moving forward is very easy. If you're not sure you'll remember how the substr() function works, just bookmark this article and return the next time you need to remove a character from a string.

Was this page useful?
PHP Delete First Character in String