PHP chop() Function with Example

By IncludeHelp Last updated : December 27, 2023

PHP chop() Function

The chop() function is a string function in PHP, it is used to remove the whitespaces or/and specified character/string from the end of the string.

Syntax

The syntax of the chop() function:

chop(string, [char/string]) : string

Parameters

The parameters of the chop() function:

  • string is the main string.
  • char/string is an optional parameter, if specifies the single character or set of characters to be removed from the end of the string. If we do not specify the char/string it removes the spaces.

Return Value

The return value of this method is string, it removes the whitespaces or/and specified character/string from the end of the string.

Sample Input/Output

Input:
str = "IncludeHelp.com#"
char/string to remove "#"
Output:
"IncludeHelp.com"
Input:
str = "IncludeHelp.com#"
char/string to remove "com#"
Output:
"IncludeHelp."

Input:
str = "IncludeHelp.com#"
char/string to remove ".com#"
Output:
"IncludeHelp"

Example of PHP chop() Function

<?php
$str = "IncludeHelp.com#";

//removing "#"
echo (chop($str, "#")."\n");
//removing "com#"
echo (chop($str, "com#")."\n");
//removing ".com#"
echo (chop($str, ".com#")."\n");
?>

Output

The output of the above example is:

IncludeHelp.com
IncludeHelp.
IncludeHelp

To understand the above example, you should have the basic knowledge of the following PHP topics:

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.