PHP addslashes() Function

P
PHP bin2hex() Function
PHP addcslashes() Function

In this article, you will learn;

  • How to add backslashes to the string.
  • Making the database unsafe queires safe by using slashes.

The PHP addslashes() method puts the backslashes in front of the following defined characters.

  • single quote (‘)
  • double quote (“)
  • backslash (\)
  • NULL

When a backslash is added to the above-defined characters, they are safe to use in the database query.

What is the syntax of the ADDSLASHES() function in PHP?

addslashes(string)
ParameterDescription
stringThe string to add the backslahes to – Required
PHP addslashes() method

Example of the ADDSLASHES() function

Example 1. In this example, we add backslashes to the specified string.

<?php
$str = addslashes('How is 'weather' tomorrow?');
echo($str);
?>

Example 1. In this example, we represent another use of addbackslash() method

<?php
$str = "I am going to Asmat's House.";
echo $str . " This is not safe in a database query";
echo addslashes($str) . " This is a database query.";
?>
PHP bin2hex() Function
PHP addcslashes() Function

Add comment

Tutor Network

Share this

Learn PHP from A to Z