PHP addslashes Function | Quote String with Slashes

PHP addslashes is an inbuilt function in PHP. It escapes a string after adding backslashes in front of pre-defined characters. Also, it does not take any specified character as input. You can un-quote a string using the PHP stripslashes Function.

The pre-defined character are:

  1. Single Quote (‘).
  2. Double Quotes (“)
  3. Backslash (\)
  4. NULL

Note: The PHP addslashes Function is different from addclashes. The former does not take any pre-defined characters while the latter does. The addcslashes function adds backslash only in front of specified characters. However, the addslashes function adds backslash in front of all the pre-defined characters.




The PHP addslashes Function expects only one parameter.

  • $string: The only parameter to the function is a string which you need to escape. In other words, this parameter specifies the string in which we want to add backslashes in front of the pre-defined characters.

Return Value

The function returns a string after escaping it with backslashes in front of all the pre-defined characters.

Input: addslashes("O'Reilly?")
Output: O\'Reilly?

Input: addslashes('Bringing "Knowledge" Together')
Output: Bringing \"Knowledge\" Together
PHP addslashes Function


Let’s discuss a few examples of using the PHP addslashes.

Example 1: Escaping Single Quote

For instance, consider a string with a single quote.


	$testString = "O'Reilly?";
	$escapedString = addslashes($testString);


The final string after escaping the single quote will be:


Example 2: Escaping Double Quotes

Similarly, we can escape a string containing double quotes.


	$testString = 'Bringing "Knowledge" Together';
	$escapedString = addslashes($testString);

Bringing \"Knowledge\" Together


In this article, we discussed the PHP addslashes Function. It escapes a string by adding backslashes in front of some pre-defined characters. You can read more about it on Official PHP Documentation. Additionally, you can learn more about PHP String Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *