Home > Backend Development > PHP Tutorial > How to use addslashes() function in PHP? (code example)

How to use addslashes() function in PHP? (code example)

青灯夜游
Release: 2023-04-06 08:28:01
Original
3639 people have browsed it

The

addslashes() function is a built-in function of PHP, which can add backslashes before predefined characters in a string. The following article will introduce to you the usage of PHP addslashes() function. I hope it will be helpful to you. [Video tutorial recommendation: PHP tutorial]

How to use addslashes() function in PHP? (code example)

PHP addslashes() function

addslashes() function Is a built-in function in PHP that returns an escaped string with a backslash in front of a predefined character. Can

Note: It will not use any of the specified characters in the parameters.

The predefined characters are:

● Single quotation mark (')

● Double quotation mark (")

● Reverse Slash (\)

● Empty (null) value

Basic syntax:

addslashes($string)
Copy after login

Parameters: The addslashes() function only accepts one parameter $string, which specifies the input string that needs to be escaped. We can also say that this parameter specifies a string in which we can add backslash before the predefined characters bar.

Return value: Returns an escaped string with backslashes, and the backslashes will be added in front of the predefined characters.

PHP addslashes() function usage example

The following is a code example to see how the addslashes() function is used.

Example 1:

<?php  
// 输入字符串
$str = addslashes(&#39;这是一个字母"p"。&#39;);  
  
// 输出
echo($str);  
?>
Copy after login

Output:

这是一个字母\"p\"。
Copy after login

Example 2:

<?php  
// 输入字符串
$str = addslashes("It&#39;s a dream!");  
  
// 输出
echo($str);  
?>
Copy after login

Output

It\&#39;s a dream!
Copy after login

Description: The addslashes() function is one of two functions that are often used to prevent injection attacks. The other function is the htmlspecialchars() function. Both functions escape special characters.

The above is the detailed content of How to use addslashes() function in PHP? (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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