PHP addcslashes() Function

Last Updated on July 22, 2021 by Roshan Parihar

PHP addcslashes() Function is used to add a backslash (\) before the specified character of a string. You have to specify a character present in a string to insert a backslash before it.

Syntax of PHP addcslashes() Function

addcslashes(string, characters)

The function two arguments in which both should be a string add a backslash in a given string in PHP.

Description of Parameters

Name Description
string Required. Specify the string that you have to use to be escaped or add a backslash before the specified characters.
characters Required. It is a character or sequence of characters of a string that you have to specify to escape in a given string. The function adds a backslash (\) before that specified character.
Note: The second argument ‘characters’ are case-sensitive. Also, be careful with the characters 0, a, b, f, n, r, t, v. The addcslashes() function converts them into \0, \a, \b, \f, \n, \r, \t, \v that are predefined escape sequences. They can give different results in the output.

Let’s see some examples that are useful to understand the use of the PHP addcslashes() Function in PHP.

Examples of PHP addcslashes() Function

Here are some useful examples to learn the function.

Escape Single and Multiple Characters to Add Backslashes

Example 1: Escape single character

When you want to escape a single character in a string, you can use the PHP addcslashes() Function as given in the example below.



The output shows that the functions escape a single character of the given string in PHP. The example adds the second argument as a single character of the string. You can also store it to a variable and pass that variable as the second argument of the function

Example 2: Escape more than one characters

Similarly, if want to escape more specified characters in a given string, you have to specify the sequence of characters as given below.



The above example escapes two specified characters in a string using the function. Likewise the above example, you can also specify more characters of a string to escape or add backslash.

Character are Case-sensitive to Specify in PHP addcslashes() Function

Example 3: Shows the character is case-sensitive

The specified character of a string in the PHP addcslashes() Function is case-sensitive. So, be careful with the capital and small letters of characters of a string in PHP.


Welcome \to Tu\torialDeep
Welcome to \TutorialDeep

The first result escaped the small ‘t’ character and the second result escaped the capital ‘T’ character in a string.

Escape Range of Characters

Example 4: Escape Range of Character in a String

In addition to the above methods, you can also escape the range of characters in a string. To escape the range of characters, you have to specify the range of characters as given in the example below.


W\el\com\e to Tutori\alD\e\ep
Welcome to Tutorial\Deep
W\e\l\c\o\m\e \t\o T\u\t\o\r\i\a\lD\e\e\p

The first line result escaped the characters from the small letter ‘a to f’. The second line result escaped the characters from capital ‘A to f’. The third line result escaped all the small characters as it is specified as ‘a to z’.

You May Also Like to Read

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.