String Functions
Advertisements

PHP addslashes() Function

Topic: PHP String ReferencePrev|Next

Description

The addslashes() function returns a string with backslashes before predefined characters. These characters are single quote ('), double quote ("), backslash (\), and the NULL-byte (\0).

The following table summarizes the technical details of this function.

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

Syntax

The basic syntax of the addslashes() function is given with:

addslashes(string);

The following example shows the addslashes() function in action.

<?php
// Sample string
$str = "Mr. O'Reilly";

// Escaping string
echo addslashes($str);
?>

Parameters

The addslashes() function accepts the following parameter.

Parameter Description
string Required. Specifies the string to be escaped.
 

Note: Prior to PHP 5.4.0, the PHP directive magic_quotes_gpc was on by default which essentially ran addslashes() on all GET, POST and COOKIE data. The addslashes() must not be used on strings that have already been escaped, otherwise it will cause double escaping. The function get_magic_quotes_gpc() can be used to check if magic_quotes_gpc is on.


More Examples

Here're some more examples showing how addslashes() function actually works:

The following example demonstrates how to escape backslash characters inside a string.

<?php
// Sample string
$str = "C:\Program Files\Common Files";

// Escaping string
echo addslashes($str);
?>
Advertisements
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties