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"
Keywords
Related Questions
- What best practices should be followed when implementing user groups to grant access for DHCP management via PHP?
- How can an upload script be integrated into a PHP page to display uploaded content?
- What are the best practices for specifying file paths in PHP scripts to avoid open_basedir restrictions and permission issues?