Use addslashes() Function in PHP 7.4 With Example

The `addslashes()` function in PHP 7.4 is used to escape special characters in a string by adding backslashes before characters like single quotes (`’`), double quotes (`”`), backslashes (`\`), and NULL characters.

This function is commonly used when inserting data into a database or when dealing with user input to prevent SQL injection or other injection attacks.

Syntax:

addslashes(string $str): string

Example in PHP 7.4:

<?php
// Original string
$input = "O'Reilly";
// Escaping special characters using addslashes
$escaped_input = addslashes($input);
echo "Original string: " . $input . "<br>";
echo "Escaped string: " . $escaped_input;
?>

Output:

Original string: O'Reilly
Escaped string: O\'Reilly

Explanation:

In this example, the `addslashes()` function adds a backslash before the single quote (`’`) in the string `O’Reilly`, turning it into `O\’Reilly`. This is useful when you want to safely insert this string into a database or process it further without worrying about SQL injection or other issues.