PHP addcslashes() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, addcslashes() returns a copy of a string with a backslash before each character in your charlist.

You pass the string and the charlist. Both are required. Use addslashes() when you only need the standard quote set.

Syntax

Call addcslashes with a string and a charlist. Start with this line.

PHP
$out = addcslashes($text, "A..z");
CallWhat it does
addcslashes("Hello", "A..z")Escapes letters in the A–z range.
addcslashes($path, "\")Escapes backslashes in a path.

Charlist supports ranges like A..z. Prefer addslashes() for the fixed quote set.

Library declarations
PHP
addcslashes(string $str, string $charlist): string

Parameters

ParameterDescription
strRequired. The input string to escape.
charlistRequired. Characters to escape. Ranges like A..z are allowed.

Return Value

Return ValueDescription
stringA new string with selected characters escaped.

PHP Examples

Example 1 — Escape a Single Character

Put a backslash before every o in the string.

PHP
<?php
$str = "Hello World!";
echo addcslashes($str, "o");
// Hell\o W\orld!

How It Works

  1. Input. $str is Hello World!.
  2. Charlist. Only the letter o should be escaped.
  3. Scan. Each o becomes \o; other characters stay the same.
  4. Result. Hell\o W\orld! — original string is unchanged.

Example 2 — Escape Single Quotes

Selective quote escaping. For the fixed quote/\/NUL set, prefer addslashes().

PHP
<?php
$name = "O'Reilly";
echo addcslashes($name, "'");
// O\'Reilly

How It Works

  1. Input. The name contains one apostrophe.
  2. Charlist. "'" means escape only single quotes.
  3. Escape. ' becomes \'.
  4. Not SQL-safe alone. For databases, use prepared statements.

Example 3 — Escape a Range (a..z)

a..z escapes every lowercase letter. Digits and punctuation stay plain.

PHP
<?php
$str = "PHP 8!";
echo addcslashes($str, "a..z");
// PHP 8!   (no lowercase letters → unchanged)

$str2 = "Code123";
echo addcslashes($str2, "a..z");
// C\o\d\e123

How It Works

  1. Range. a..z means every lowercase ASCII letter.
  2. First string. PHP 8! has no lowercase letters → unchanged.
  3. Second string. In Code123, uppercase C stays plain; o, d, and e become \o, \d, \e.
  4. Careful. Prefer a..z / A..Z separately—avoid A..z unless you want the symbols in between.

Frequently Asked Questions

addcslashes() returns a new string with a backslash inserted before every character listed in the charlist argument. You choose exactly which characters to escape, unlike addslashes() which always escapes quotes, backslashes, and the NUL byte.
string addcslashes(string $str, string $charlist). The first parameter is the input string; the second is a list of characters (or ranges) to escape with a backslash.
Use two dots between endpoints, such as a..z or A..Z. The range follows ASCII order. Beware of A..z—it also includes characters between Z and a, like [ \ ] ^ _ `.
addslashes() always escapes single quotes, double quotes, backslashes, and NUL. addcslashes() lets you pick any characters via charlist. Use addcslashes() for selective escaping.
No. Use prepared statements (PDO or MySQLi) for database queries. addcslashes() is for general string escaping in custom contexts—not a substitute for proper SQL handling.

Next: addslashes()

Learn the fixed-set escaper for quotes, backslash, and NUL.

addslashes tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful