What is the purpose of the addslashes() function in PHP and how does it differ from stripslashes()?

The addslashes() function in PHP is used to escape special characters in a string, making it safe to insert into a database to prevent SQL injection attacks. On the other hand, stripslashes() is used to remove slashes added by the addslashes() function or any other escaping mechanism.

// Example of using addslashes() to escape special characters before inserting into a database
$name = "John O'Connor";
$escaped_name = addslashes($name);
// $escaped_name now contains "John O\'Connor"

// Example of using stripslashes() to remove slashes added by addslashes()
$original_name = stripslashes($escaped_name);
// $original_name now contains "John O'Connor"