In PHP 7.4, the `chop()` function is an alias for the `rtrim()` function. It is used to remove whitespace or other predefined characters from the right (end) side of a string.
Syntax:
chop(string $str, string $character_mask = " \t\n\r\0\x0B"): string
$str: The input string.
$character_mask (optional): You can specify which characters to remove. If not provided, it removes whitespace characters.
Example 1: Removing Trailing Whitespace
<?php $string = "Hello, World! "; $trimmed_string = chop($string); echo "Original: '$string'\n"; echo "Trimmed: '$trimmed_string'\n"; ?>
Output:
Original: 'Hello, World! ' Trimmed: 'Hello, World!'
Example 2: Removing Specific Characters
<?php $string = "Hello, World!!!"; $trimmed_string = chop($string, "!"); echo "Original: '$string'\n"; echo "Trimmed: '$trimmed_string'\n"; ?>
Output:
Original: 'Hello, World!!!' Trimmed: 'Hello, World'
In this example, the `chop()` function removes the exclamation marks (`!`) from the right side of the string.