Content · Programming · PHP

PHP addcslashes() Function: Usage and Examples

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

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

Adds C-style backslashes before selected characters.

What Is addcslashes() in PHP?

Adds C-style backslashes before selected characters.

Its syntax is:

addcslashes(string $string, string $characters): string

Parameters:

  • $string — Accepted as string; required argument.
  • $characters — Accepted as string; required argument.

Return type: string. The characters argument supports ranges such as A..Z. It does not provide SQL or HTML escaping; use appropriate context-specific APIs.

Availability: Available in standard PHP.

Example 1: Basic Usage

<?php

echo addcslashes("a.b.c", ".");

Output:

a\.b\.c

The characters argument supports ranges such as A..Z. It does not provide SQL or HTML escaping; use appropriate context-specific APIs.

Example 2: A Different Input

<?php

echo addcslashes("A\tB", "A");

Output:

\A	B

The characters argument supports ranges such as A..Z. It does not provide SQL or HTML escaping; use appropriate context-specific APIs.

Practical Example: Applying It in a Program

<?php

$identifiers = ["item.1", "item.2"];
foreach ($identifiers as $identifier) { echo addcslashes($identifier, ".") . PHP_EOL; }

Output:

item\.1
item\.2

The characters argument supports ranges such as A..Z. It does not provide SQL or HTML escaping; use appropriate context-specific APIs.

Important Behavior and Edge Cases

  • Key behavior: The characters argument supports ranges such as A..Z. It does not provide SQL or HTML escaping; use appropriate context-specific APIs.
  • Environment: Available in standard PHP.

Comparison With Related Functions

FunctionPurpose
addcslashes()adds C-style backslashes before selected characters.
addslashes()addslashes() escapes a fixed group of quotation and backslash characters; addcslashes() uses a configurable character list.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: The characters argument supports ranges such as A..ZThe characters argument supports ranges such as A..Z. It does not provide SQL or HTML escaping; use appropriate context-specific APIs.

Key Takeaways

  • adds C-style backslashes before selected characters.
  • The characters argument supports ranges such as A..Z. It does not provide SQL or HTML escaping; use appropriate context-specific APIs.
  • Available in standard PHP.

Summary: The characters argument supports ranges such as A..Z. It does not provide SQL or HTML escaping; use appropriate context-specific APIs.

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