The addslashes() function is used to add backslashes in front of the predefined characters of string.
Note: In PHP \0 (NULL), \r (carriage return), \n (newline), \f (form feed), \v (vertical tab) and \t (tab) are predefined escape sequences.
PHP addslashes() Examples :
<?php $greeting = "Hello, I'm John"; print addslashes($greeting); ?> Output: Hello, I\'m John
<?php $greeting = 'What is "PHP"?'; print addslashes($greeting); ?> Output: What is \"PHP\"?
Predefined character details are given below :
- single quote (‘)
- double quote (“)
- backslash (\)
Tip: This function can be used to prepare a string for storage in a database and database queries.
Note: PHP runs addslashes() on all GET, POST, and COOKIE data by default. Therefore you should not use addslashes() on strings that have already been escaped, this will cause double escaping. The function get_magic_quotes_gpc() can be used to check this.