How does the addslashes() function work in PHP and what potential pitfalls should be aware of when using it?

The addslashes() function in PHP is used to escape special characters in a string, particularly characters that could potentially be used for SQL injection attacks. It adds a backslash before characters like single quotes, double quotes, backslashes, and NULL bytes. When using addslashes(), be cautious of over-escaping data, as it can lead to unintended consequences such as double escaping or data corruption.

// Example of using addslashes() function to escape special characters in a string
$input = "It's a beautiful day!";
$escaped_input = addslashes($input);

echo $escaped_input;