Updated April 3, 2023
Introduction to PHP chop()
If there are any whitespaces or any other characters that are predefined in a string, a function called chop() function can be used to remove them from the right side of the string which is also an alias for a function called rtrim() function in PHP. This function is supported in the fourth version and the above versions after fourth version of PHP and the return value of this function in PHP is string but is a changed string when compared to the input string passed to the function.
Syntax
The syntax to declare chop() function in PHP is as follows:
chop(string_name, char_list)
where,
- string_name: Is the name of the string that requires trimming, that is the chop() function must be applied on. This is the essential parameter of the syntax and is mandatory. The data type of this parameter is string data type.
- char_list: It specifies the list of characters to be removed from the string that is passed as a parameter. If the char_list parameter is not specified in the chop() function that is being called, the chop() function will be applied on the following list of characters like an ordinary space, \0” which is also called as Null byte, \t” which is also called as a tab, \n” which is also called as line feed, \x0B” which is also called as a vertical tab, “\r” which is also called as a carriage return. This is not an essential parameter of the syntax and is optional. The data type of this parameter is string data type.
How does chop() Function work in PHP?
Chop() function is one of the string functions in PHP. The required whitespaces or specified characters or string from the right side of the string can be removed using chop() function in PHP.
Consider the below program:
Code:
<?php
//declaring the string Shobha.Shivakumar#
$string = "Shobha.Shivakumar#";
//Using chop() function to remove the character # from the right side of the string
echo (chop($string, "#")."\n");
//Using chop() function to remove the characters akumar# from the right side of the string
echo (chop($string, "akumar#")."\n");
//Using chop() function to remove the characters .Shivakumar# from the right side of the string
echo (chop($string, "ha.Shivakumar#")."\n");
?>
Output:
Explanation:
- In the above program, a string is declared upon which the chop() function in PHP must be applied. The string declared in the above program is Shobha.Shivakumar#. By using chop() function in PHP, we can remove the whitespaces or the specific characters from the right side of the string. In our program, we apply the chop function on the string to remove the # character from the string Shobha.Shivakumar#.
- An echo statement in PHP is used to print the changed string after the application of the chop() function on the given string. Echo (chop($string, “#”).”\n”); statement is used in PHP to perform this operation and the output of this is Shobha.Shivakumar which can be seen in the snapshot. We then apply the chop function on the string to remove the akumar# characters from the string Shobha.Shivakumar#. An echo statement in PHP is used to print the changed string after the application of the chop() function on the given string. echo (chop($string, “akumar#”).”\n”); statement is used in PHP to perform this operation and the output of this is Shobha.Shiv which can be seen in the snapshot.
- We then apply the chop function on the string to remove the ha.Shivakumar# characters from the string Shobha.Shivakumar#. An echo statement in PHP is used to print the changed string after the application of the chop() function on the given string. echo (chop($string, “Shivakumar#”).”\n”); statement is used in PHP to perform this operation and the output of this is Shob which can be seen in the snapshot. “\n” is used in every echo statement in PHP to print the output of each line in a new line. The same operation can be performed on different character on different types of strings.
Examples of PHP chop()
Given below are the examples:
Example #1
PHP program to illustrate the working of chop() function on strings.
Code:
<?php
//declaring the string Shobha.Shivakumar!
$string= "Shobha.Shivakumar!";
//printing the string on which the chop() function will be applied and making sure the next statement will be printed in a new line
echo $string. "\n";
//printing the changes string after the application of the input string
echo chop($string, "r!");
?>
Output:
Explanation:
- In the above program, a string is declared upon which the chop() function in PHP must be applied. The string declared in the above program is Shobha.Shivakumar!. By using chop() function in PHP, we can remove the whitespaces or the specific characters from the right side of the string. In our program, we apply the chop function on the string to remove the characters r! from the string Shobha.Shivakumar!
- An echo statement in PHP is used to print the changed string after the application of the chop() function on the given string. echo (chop($string, “r!”); statement is used in PHP to perform this operation and the output of this is Shobha.Shivakuma which can be seen in the snapshot. “\n” is used in the second echo statement in PHP to sure the next statement will be printed in a new line. The same operation can be performed on different character on different types of strings.
Example #2
PHP program to illustrate the working of chop() function on strings.
Code:
<?php
//declaring the string Hello! followed by Shobha Shivakumar in a new line, the string is followed by two new line characters
$string= "Hello! \n Shobha Shivakumar \n \n";
//the string is printed
echo $string;
//chop() function is applied on the string without specifying any parameter
echo chop($string);
//The changed string is printed
echo $string;
?>
Output:
Explanation:
- In the above program, a string is declared upon which the chop() function in PHP must be applied. The string declared in the above program is Hello! \n Shobha Shivakumar \n \n. By using chop() function in PHP, we can remove the whitespaces or the specific characters from the right side of the string. In our program, we apply the chop function on the string without specifying any parameters.
- An echo statement in PHP is used to print the changed string after the application of the chop() function on the given string. echo $string; statement is used in PHP to perform this operation and the output of this is Hello! Followed by Shobha Shivakumar in a new line and two new line spaces after that can be seen in the snapshot. “\n” is used in the echo statement in PHP to sure the next statement will be printed in a new line. The output after the application of chop() function without specifying the parameters removes the two line characters from the right side of the string.
Recommended Articles
This is a guide to PHP chop(). Here we discuss the Introduction, syntax, and working of the chop() function in PHP along with various examples. You may also have a look at the following articles to learn more –