What is the addcslashes function for in php?

Asked

Viewed 278 times

0

What is the addcslashes function for? I saw in the PHP manual but did not understand.

1 answer

1


According to the manual:

Returns a string with backslashes before the characters that are listed in the charlist parameter.

Example:

php > echo addcslashes("Ola! Meu nome é Homer. Tudo bem?", "!.?");
Ola\! Meu nome é Homer\. Tudo bem\?

Normally the escape character () is used when you want to change the original character direction.

For example, if you want to print the " n". The character has a special meaning (escape the following character). In this case, it transforms the sequence into a line change. If it is executed:

echo "\n";

A line change will be printed. If you want to print (literally) n, you need to use n. It will be interpreted as follows:

: Look, the next character doesn’t have the usual sense.

: My usual sense is to escape the next character, but the previous character changed my meaning. Then I will be interpreted as just a bar.

n: I am just an n.

And as a result it will be printed " n".

Returning to the addcslashes

You specify the string and which characters you want to insert in the front so as to change its usual sense. Usually used to escape strings for printing or processing.

  • I understand, but what is the logic of using this function? or whatever the occasions it is used, I am a student in PHP and I see a lot in the PHP codes that I am studying.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.