The addslashes() function is an inbuilt function in PHP and it returns a string with backslashes in front of predefined characters. It does not take any specified characters in the parameter.
The predefined characters are:
- single quote (‘)
- double quote (“)
- backslash (\)
- NULL
Note: The addslashes() function is different from addcslashes() function accepts specified characters before which be want to add slashes but the addslashes() function doesnot accepts any character in parameters, rather it adds slashes before some specified characters.
Syntax:
addslashes($string)
Parameters: The addslashes() function accepts only one parameter $string which specifies the input string which is needed to be escaped. We can also say that this parameter specifies a string in which we want to add backslashes before the pre-defined characters.
Return value: It returns the escaped string with backslashes in front of the pre-defined characters which is passed in the parameter.
Examples:
Input : $string = "Geek's" Output : Geek\'s Input : $string='twinkle loves "coding"' Output : twinkle loves \"coding\"
Below programs illustrate the addslashes() function in PHP:
Program 1:
<?php // PHP program to demonstrate the // working of addslashes() function // Input String $str = addslashes('twinkle loves "coding"'); // prints the escaped string echo($str); ?> |
Output:
twinkle loves \"coding\"
Program 2:
<?php // PHP program to demonstrate the // working of addslashes() function // Input String $str = addslashes("Geek's"); // prints the escaped string echo($str); ?> |
Output:
Geek\'s
Reference:
http://php.net/manual/en/function.addslashes.php
Recommended Posts:
- How to Check a Function is a Generator Function or not using JavaScript ?
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- PHP | cos( ) Function
- PHP | Ds\Set contains() Function
- D3.js | d3.lab() Function
- PHP | sin( ) Function
- PHP | dir() Function
- D3.js now() function
- PHP | Ds\Map first() Function
- PHP Ds\Map sum() Function
- D3.js | d3.hcl() Function
- PHP | tan( ) Function
- PHP | ord() Function
- PHP | Ds\Set xor() Function
- D3.js | d3.max() function
- CSS | rgb() Function
- PHP | Ds\Set add() Function
- p5.js | nf() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

