addslashes
Adds a backslash before quotes.
Syntax
addslashes(string $string): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $string | string | The input string. |
Return value
stringThe string with `'`, `"`, `\` and the null byte escaped.
Examples
A quote inside a string:
<?php
echo addslashes("It's a \"quote\"");Output:
It\'s a \"quote\"Notes
The function does not protect against SQL injection — use prepared statements for queries. To put data into JavaScript use json_encode().
See also
- htmlspecialchars — Replaces HTML special characters with safe entities.
- json_encode — Converts a value to a JSON string.
- PDOStatement — A prepared statement and its result.
In the tutorial
- Security — The basics of PHP site security — XSS and escaping, SQL injection, password hashing, CSRF tokens and validating input.