Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
web development
communitycreator

What is the addslashes() function in PHP

NDUKWE CHIDERA K.

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

To use special symbols and characters as part of your string’s unpredictable behavior, you have to add backslashes just before the character.

You can either do this manually or by using the addslashes() function. The addslashes() function automatically adds backslashes to your string.

Syntax

addslashes($string);

Parameters

The function requires only one parameter which is the input string with characters to be escaped.

Support

This function is available in PHP version 4 and higher.

Return value

The function returns a string with escaped characters. These characters include:

  • Double quotes (")
  • Single quotes (')
  • Backslash (\)

Code

The code below shows the use of the addslashes() function:

<?php

$string=('"He said you cannot come "Home yet" with me"');

//escaping the variable $string;
$escString = addslashes($string);

//printing it to output
echo $escString."\n";

//escaping the $str 
$str = addslashes('(\)\'');

// printing it to the screen
echo $str;

?>

Try not to use only this function for form input cleaning to prevent SQL injections.

RELATED TAGS

php
web development
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring