PHP addslashes() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, addslashes() returns a copy of a string with a backslash before ', ", \, and the NUL byte.

You pass one string. It is required. This is not a substitute for prepared statements against SQL injection.

Syntax

Call addslashes on the string. Start with this line.

PHP
$escaped = addslashes($text);
CallWhat it does
addslashes("O'Reilly")Returns O\'Reilly.
stripslashes($escaped)Removes the added slashes.

For HTML output use htmlspecialchars(). For SQL use prepared statements.

Library declarations
PHP
addslashes(string $str): string

Parameters

ParameterDescription
strRequired. The input string to escape.

Return Value

Return ValueDescription
stringA new string with the four special characters escaped.

PHP Examples

Example 1 — Escape a Single Quote

The apostrophe in a name becomes \'.

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

How It Works

  1. Input. $name contains one apostrophe.
  2. Scan. addslashes() looks for ', ", \, and NUL.
  3. Escape. The apostrophe becomes \'.
  4. 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

How It Works

  1. Source string. In PHP source, \" is a double quote and \\ is one backslash.
  2. Actual value. The string holds: He said "Hi" and used C:\Temp.
  3. Escape quotes. Each " becomes \".
  4. 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

How It Works

  1. Scan. No ', ", \, or NUL appears.
  2. No changes. Every character is copied as-is.
  3. Same text. Output matches the original string.
  4. 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.

Next: bin2hex()

Learn how to convert binary data to a hexadecimal string.

bin2hex 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