Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP addslashes() Function

PHP addslashes() Function

PHP String Reference Manual PHP String Reference Manual

Example

Add a backslash before each double quote ("):

Run Example »


Definition and Usage

The addslashes() function returns a string with backslashes added before predefined characters.

The predefined characters are:

**Tip:** This function can be used to prepare suitable strings for strings stored in a database as well as database query statements.

**Note:** By default the PHP directive magic_quotes_gpc is on, which automatically runs addslashes() on all GET, POST and COOKIE data. Do not use addslashes() on a string that has already been escaped by magic_quotes_gpc, because this will cause double escaping. In such cases you can use the get_magic_quotes_gpc() function to detect it.


Syntax

addslashes(

string

)

Parameters Description
string Required. Specifies the string to escape.

Technical Details

Return Value: Returns the escaped string.
PHP Version: 4+

More examples

Example 1

Add backslashes before the predefined characters in a string:

"; echo addslashes($str) . " This is safe in a database query."; ?>

Run Example »


PHP String Reference Manual PHP String Reference Manual