Home >Backend Development >PHP Problem >php addslashes escape method
php addslashes escape method: first create a PHP sample file; then define a string; finally add backslashes to the predefined characters in the string through "addslashes($str)".
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
addslashes definition and usage
addslashes() function returns a string with backslashes added before predefined characters.
The predefined characters are:
Single quote (')
Double quote (")
Backslash (\ )
NULL
Tip: This function can be used to prepare strings for strings stored in the database and database query statements.
Note: By default, PHP GET, POST and COOKIE data automatically runs addslashes(). So you should not use addslashes() on escaped strings, as this will cause double-level escaping. When encountering this situation, you can use the function get_magic_quotes_gpc() Perform detection.
Syntax
addslashes(string)
Parameters
string Required. Specifies the string to be escaped.
Technical details
Return Value: Returns the escaped string.
PHP Version: 4
Example
Add backslash to predefined characters in a string Bar:
<?php $str = "Who's Bill Gates?"; echo $str . " This is not safe in a database query.<br>"; echo addslashes($str) . " This is safe in a database query."; ?>
Output:
Who's Bill Gates? This is not safe in a database query. Who\'s Bill Gates? This is safe in a database query.
[Recommended learning: "PHP Video Tutorial"]
The above is the detailed content of php addslashes escape method. For more information, please follow other related articles on the PHP Chinese website!