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
| Function | Purpose |
|---|---|
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
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: The characters argument supports ranges such as A..Z | The 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.