Home >>PHP String Functions >PHP addslashes() Function

PHP addslashes() Function

PHP addslashes()

PHP addslashes() function is used to add slashes before certain characters of a given input string.PHP addslashes() is an inbuilt function of PHP. This function doesn't take any specific character in the parameter. The characters before which the backslash can be added are pre-defined.Those pre-defined characters are single quote ('), double quote ("), and the NULL character.

Syntax:

addslashes($string)

$string is the given input string in which we have to add the backslash before any pre-defined character. The resultant string will be same as the input string but with a backslash before the specific character.

Let's see this with an example:

<html>
<body>
<?php 
$str = "This is '2020' now";
echo addslashes($str); 
?> 
</body>
</html>
Output:
This is /2020/ now

More Examples:-

<html>
<body>
<?php 
$str = "This is how we can\ add \multiple backslashes' in a 'single' string";
echo addslashes($str); 
?> 
</body>
</html>
Output:
This is how we can\\ add \\multiple backslashes\' in a \'single\' string
<html>
<body>
<?php 
$str1 = "Hello \World...";
$str2 = "Hello 'World...";
$str3 = 'Hello "World...';
echo addslashes($str1)."<br>";
echo addslashes($str2)."<br>";
echo addslashes($str3)."<br>";
?> 
</body>
</html>
Output:
Hello \\World...
Hello \'World...
Hello \"World...

Popular Tutorials