Result. Letters stay the same; only the special char changes.
Example 2 — Quotes and Backslash Together
Every special character in the fixed set gets its own backslash.
PHP
<?php
$str = "He said \"Hi\" and used C:\\Temp";
echo addslashes($str);
// He said \"Hi\" and used C:\\Temp
Output
He said \"Hi\" and used C:\\Temp
How It Works
Source string. In PHP source, \" is a double quote and \\ is one backslash.
Actual value. The string holds: He said "Hi" and used C:\Temp.
Escape quotes. Each " becomes \".
Escape backslash. Each \ becomes \\ (shown as \\ in the printed result).
Example 3 — Nothing to Escape
If the string has none of the four special characters, the result equals the input.
PHP
<?php
$str = "Hello World";
echo addslashes($str);
// Hello World
Output
Hello World
How It Works
Scan. No ', ", \, or NUL appears.
No changes. Every character is copied as-is.
Same text. Output matches the original string.
Still useful. Safe to call when data might contain special chars.
Frequently Asked Questions
addslashes() returns a new string with a backslash inserted before single quotes, double quotes, backslashes, and the NUL (null) byte. The original string is never modified.
string addslashes(string $str). You pass one string argument and receive an escaped copy.
Exactly four: single quote ('), double quote ("), backslash (\), and the NUL byte. No other characters are affected.
addslashes() always escapes the same four characters. addcslashes() lets you choose any characters via a charlist. Use addslashes() for standard quote escaping; use addcslashes() for selective control.
No. Use prepared statements (PDO or MySQLi) for database queries. addslashes() alone is not a reliable SQL injection defense.