The opposite of stripslashes(), this function attempts to add backslashes where needed to escape a string.

Also as with stripslashes(), if magic_quotes_sybase is set to on, this function will instead double up the single quotes to escape them.

For this example, I’ll use Heredoc since we have both types of quotes in the string. And note that the backslash of \n isn’t bothered since that is a special character (new line) here.

$text = <<<EndText
I'm Henry the Eighth I am.\n"Henry the Eighth" I am, I am.
EndText;
 
$new = addslashes($text);
/*
I\'m Henry the Eighth I am.
\'Henry the Eighth\' I am, I am.
*/
 
ini_set('magic_quotes_sybase', 1);
$new = addslashes($text);
/*
I''m Henry the Eighth I am.
"Henry the Eighth" I am, I am.
*/

This function is commonly used to escape a string for insertion into a database. Before using if, check if your database has a function specific for it. For MySQL or PostgreSQL, mysql_real_escape_string() or pg_escape_string() respectively are better suited for the job.