Friday, October 10, 2025
HomeLanguagesPHP addslashes() Function

PHP addslashes() Function

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:

  1. single quote (‘)
  2. double quote (“)
  3. backslash (\)
  4. NULL

Note: The addslashes() function is different from addcslashes() function accepts specified characters before which we want to add slashes but the addslashes() function does not 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
// 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 
// 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

RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6717 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6792 POSTS0 COMMENTS