Home >Backend Development >PHP Tutorial >The difference between daddslashes() and saddslashes() in php

The difference between daddslashes() and saddslashes() in php

WBOY
WBOYOriginal
2016-07-25 09:03:31965browse
  1. //----saddslashes

  2. function daddslashes($string, $force = 0, $strip = FALSE) {
  3. //Is the string or array forced? Remove
  4. //If magic reference is not turned on or $force is not 0
  5. if(!MAGIC_QUOTES_GPC || $force) {
  6. if(is_array($string)) { //If it is an array, loop this function
  7. foreach ($string as $key => $val) {
  8. $string[$key] = daddslashes($val, $force);
  9. }
  10. } else {
  11. //If magic references are on or $force is 0
  12. / /The following is a ternary operator. If $strip is true, execute stripslashes to remove the backslash character, and then execute addslashes
  13. //$strip is true, that is, remove the backslash character first and then escape it as $ _GET, $_POST, $_COOKIE and $_REQUEST The $_REQUEST array contains the values ​​​​of the first three arrays
  14. //Why do we need to remove the backslash and then escape the $string here, because sometimes $string may have Two backslashes, stripslashes filters out excess backslashes
  15.   $string = addslashes($strip ? stripslashes($string) : $string);
  16. }
  17. }
  18. return $string;
  19. }
  20. //------saddslashes

  21. function saddslashes($string) { if(!MAGIC_QUOTES_GPC){
  22. if(is_array($string)) { //If the escape is an array, then the array Recursively escape the value in
  23.    foreach($string as $key => $val) {
  24.    $string[$key] = saddslashes($val);
  25.    }
  26. } else {
  27.   $string = addslashes($string ); //Escape single quotes ('), double quotes ("), backslashes () and NUL (NULL characters)
  28. }
  29. return $string;
  30. }else{
  31. return $string;
  32. }
  33. ?>

Copy the code

The key point is: saddslashes can escape every data:

  1. function saddslashes($string) {
  2. if(is_array($string)) {
  3. foreach($string as $key => $val) {
  4. $string[$key] = saddslashes($val);
  5. }
  6. } else {
  7. $string = addslashes($string);
  8. }
  9. return $string;
  10. }
  11. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn