Removes selected characters from the end of a string; it is an alias of rtrim().
What Is chop() in PHP?
Removes selected characters from the end of a string; it is an alias of rtrim().
Its syntax is:
chop(string $string, string $characters = " \n\r\t\v\0"): string
Parameters:
$string— Accepted as string; required argument.$characters— Accepted as string; optional default " \n\r\t\v\0".
Return type: string. The character mask is a set of characters, not a literal suffix.
Availability: Core PHP.
Example 1: Basic Usage
<?php
echo "[" . chop("ready ") . "]";
Output:
[ready]
The character mask is a set of characters, not a literal suffix.
Example 2: A Different Input
<?php
echo chop("file.csv", "vs.");
Output:
file.c
The character mask is a set of characters, not a literal suffix.
Practical Example: Applying It in a Program
<?php
$lines = ["alpha\n", "beta\r\n"]; foreach ($lines as $line) { echo "[" . chop($line) . "]" . PHP_EOL; }
Output:
[alpha]
[beta]
The character mask is a set of characters, not a literal suffix.
Important Behavior and Edge Cases
- Key behavior: The character mask is a set of characters, not a literal suffix.
- Environment: Core PHP.
Comparison With Related Functions
| Function | Purpose |
|---|---|
chop() | Removes selected characters from the end of a string; it is an alias of rtrim(). |
rtrim() | chop() is exactly an alias of rtrim(); the two names have the same behavior. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: The character mask is a set of characters, not a literal suffix | The character mask is a set of characters, not a literal suffix. |
Key Takeaways
- Removes selected characters from the end of a string; it is an alias of rtrim().
- The character mask is a set of characters, not a literal suffix.
- Core PHP.
Summary: The character mask is a set of characters, not a literal suffix.