Home > Backend Development > PHP Tutorial > Solution to error reported using php addslashes function

Solution to error reported using php addslashes function

怪我咯
Release: 2023-03-07 20:28:01
Original
1653 people have browsed it

The addslashes() function in php is to add a backslash before some predefined characters in the input string. However, if you find that you use the php addslashes function to report an error, Then you need to take a good look at your code. If you don’t believe me, please look at the code below?

<?php
echo function_exists(&#39;addcslashes&#39;);//检测是否存在该函数
echo get_magic_quotes_gpc().&#39;<br/>&#39;;//检测是否开启自动转义
echo PHP_VERSION.&#39;<br/>&#39;;//php版本信息
echo addcslashes("Who&#39;s John Adams?");
echo &#39;<br/>&#39;;
$str = "Who&#39;s John Adams?";
echo addslashes($str);
?>
Copy after login

What will be the result of running the above code?

The running result of the code:

Solution to error reported using php addslashes function

We can see from the above running result:

One reports an error and the other outputs normally. why?

ps: If there is no error message in your running results, you need to set your PHP error level to see it. For how to set the PHP error level, please refer to this article: How to set PHP Error level?

Because addslashes and addcslashes are not the same function, I found that there is no difference, only one character. But both functions do the same thing - escape strings.

What is the difference between addslashes and addcslashes?

addslashes has only one parameter - string

addcslashes requires two parameters, string, delimiter.

echo addcslashes("Who&#39;s John Adams?","&#39;");
Copy after login

Writing like this will allow normal output.

The code is as follows:

<?php
echo function_exists(&#39;addcslashes&#39;);//检测是否存在该函数
echo get_magic_quotes_gpc().&#39;<br/>&#39;;//检测是否开启自动转义
echo PHP_VERSION.&#39;<br/>&#39;;//php版本信息
echo addcslashes("Who&#39;s John Adams?","");
echo &#39;<br/>&#39;;
$str = "Who&#39;s John Adams?";
echo addslashes($str);
?>
Copy after login

Code running results:

Solution to error reported using php addslashes function

Summary: I hope this article can help everyone understand the usage of addslashes.

1.php Detailed explanation of addslashes() function and stripslashes() function examples

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

The above is the detailed content of Solution to error reported using php addslashes function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template