Content · Programming · PHP

PHP chop() Function: Usage and Examples

Learn how PHP chop works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

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

FunctionPurpose
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

MistakeCorrect Approach
Ignoring this behavior: The character mask is a set of characters, not a literal suffixThe 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.

Share this resourceSend this page to someone who may find it useful.