PHP addslashes() Function
PHP addslashes() function is used to return a quote string with slashes. It works with some characters:
- Single quote (')
- Double quote(")
- Blackslash(|)
- NUL (the NUL byte)
Syntax:
string addslashes ( string $str );
Parameter | Description | Required/Optional |
---|---|---|
String | String to be escaped | Required |
Example 1
<?php
$str = 'What does "WHO" mean?';
echo "Your string is :".$str;
echo "<br>"."By using addslashes() function the result is".addslashes($str);
?>
Output
Your string is :What does "WHO" mean?
By using addslashes() function the result isWhat does "WHO" mean?
Example 2
<?php
$str = "Who's the father of PHP?";
echo $str . " This is not safe in a database query.<br>";
echo addslashes($str) . " This is safe in a database query.";
?>
Output
Who's the father of PHP? This is not safe in a database query.
Who's the father of PHP? This is safe in a database
Example 3
<?php
$str = "Wow' PHP?";
eval("echo '" . addslashes($str) . "';");
>?
Output
Wow' PHP?
Example 4
<?php
$str = "Is The Father of PHP'Rasmus?";
//Is The Father of PHP'Rasmus?
echo addslashes($str);
?>
Output
Is The Father of PHP'Rasmus?