PHP addslashes

 The PHP addslashes function is used to add backslashes in front of some symbols in the string, such as single quotes, double quotes, backslashes or NULL characters. It is usually used to add special characters through PHP addslashes before database retrieval. After the slash, avoid conflicts with SQL syntax or syntax extraction problems.


Basic syntax of PHP addslashes function

String addslashes( $string)


Put the string $string to be converted into parentheses, let the addslashes function perform the conversion, and return the converted result. If the $string does not contain single quotes, double quotes, backslashes or NULL characters, the original is returned $string, which means that the addslashes function does not change the original appearance of the string.

PHP addslashes function usage example
<?php
 $string="Thank's for your help.";
 echo addslashes($string).'<br>';

 $nwe_string=addslashes($string);
 echo addslashes($nwe_string);
?>
The above output
Thank\'s for your help.
Thank\\\'s for your help.
From this example, you can see that addslashes can add a backslash before the single quotation mark (') and backslash (\) of the string. The first output only has a single quotation mark, so a backslash is added, and the second time is processed The $new_string addslashes once, so there is 1 single quotation mark and 1 backslash, and the final output result is 2 more backslashes, so there are 3 backslashes. In principle, addslashes can always add backslashes repeatedly. If you want to shift In addition to backslashes, you can use the PHP stripslashes function.

Post a Comment

0 Comments