Workarounds for error using PHP addslashes function

Source: Internet
Author: User
The Addslashes () function in PHP is to add a backslash before some of the predefined characters in the input string, but if you find yourself using PHP addslashes function error,Then you need to take a good look at your code. If you don't believe me, please look at the code below.

<?phpecho function_exists (' addcslashes ');//detects if the function is present in echo GET_MAGIC_QUOTES_GPC (). ' <br/> ';//detect if Automatic escape echo php_version is turned on. ' <br/> ';//php version information echo addcslashes ("Who's John Adams?"); Echo ' <br/> '; $str = "Who's John Adams?"; echo addslashes ($STR);? >

What is the result of the above code running?

The result of the code operation:

We can see from the above running results:

One error, another normal output. Why is it?

PS: If you run the results without error message, you need to set up your PHP error level can see, about how to set the error level of PHP, please refer to this article: PHP How to set the error level?

Because Addslashes and addcslashes are not the same function, it is found that there is only one character difference. But the functions of the two functions are the same-escaping the string.

What is the difference between addslashes and addcslashes?

Addslashes only one parameter-string

Addcslashes requires two parameters, a string, and a delimiter.

Echo Addcslashes ("Who s John Adams?", "'");

This will allow the normal output of the write.

The code is as follows:

<?phpecho function_exists (' addcslashes ');//detects if the function is present in echo GET_MAGIC_QUOTES_GPC (). ' <br/> ';//detect if Automatic escape echo php_version is turned on. ' <br/> ';//php version information echo addcslashes ("Who's John Adams?", ""); Echo ' <br/> '; $str = "Who's John Adams?"; echo addslashes ($STR);? >

Code Run Result:

Summary: Hope this article can help you understand the use of addslashes.

1.php addslashes () function and stripslashes () function Example

Examples of the difference between the 2.php stripslashes () function and the addslashes () function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.