Thursday, 25 September 2014

addslashes in PHP

PHP addslashes() function is utilized to includes backslashes in front of predefined characters in a string.
The predefined characters are:
  • single quote (')
  • double quote (")
  • backslash (\)
  • NULL

Syntax:

addslashes(string)
 
Parameter Description: 
string : Required.  Define the input string.
Tip : function could be utilized to get ready a string for space in a database and database queries.
Note: PHP runs addslashes() function on all GET, POST, and COOKIE data by default.

Example:

<?php 
echo "<strong>Adding backslashes to predefined characters in a string :</strong><br />";
$str = "Who's Mark Johnson?";
echo $str." This is not safe in a database query.<br />";
echo addslashes($str)." This is safe in a database query.";
?>

O/P:

Adding backslashes to predefined characters in a string :
Who's Mark Johnson? This is not safe in a database query.
Who\'s Mark Johnson? This is safe in a database query.

0 comments:

Post a Comment