addslashes() - add a backslash to PHP string special characters
The PHP addslashes() function is used to add backslashes to these special characters:
- single quote (
') - double quote (
") - backslash (
\) NULLvalue
addslashes(string $string): string
addslashes() accepts a string as its parameter and will return that string with backslashes added.
This function is commonly used to escape the special characters from getting evaluated.
For example:
$str = 'PHP is a "server-side" programming language';
echo addslashes($str);
// 👆 PHP is a \"server-side\" programming language
While this function can be used to prepare your string for database storage or query, it’s not recommended to use it.
This is because databases have their own specific escaped functions, like mysql_real_escape_string for mySQL and pg_escape_string() for PostgreSQL.
addslashes() doesn’t make your query string safe for use as database query, as it only escapes the four characters mentioned above.
More PHP tutorials
See allPHP
PHP form validation basic example
Dec 22, 2022
PHP
PHP validate email address (with code examples)
Dec 22, 2022
PHP
How to redirect using PHP with code examples
Dec 22, 2022
PHP
Handling form data using PHP (GET and POST)
Dec 21, 2022
PHP
How to fix Wordpress PHP error missing MySQL extension
Dec 21, 2022