How can special characters like ' or " be prohibited in a PHP string variable?

Special characters like ' or " can be prohibited in a PHP string variable by using the addslashes() function to escape these characters. This function adds a backslash before these special characters, preventing them from being interpreted as part of the string. By using addslashes() before assigning a value to the string variable, you can ensure that these special characters are not allowed.

$string = "This is a string with ' and \" characters.";
$clean_string = addslashes($string);
echo $clean_string;