phpphp-7php-5.3addslashes

addcslashes(), charlist parameter - Is the explanation on php.net correct?


When I run this piece of code:

echo addcslashes('testing\n', "\n");

It outputs this:

"testing\n"

The explanation of Parameters -> Charlist says:

If charlist contains characters \n, \r etc., they are converted into C-like style

but this does not seem to be correct. This would mean that "\n" gets converted to "\n" in C-like style which would not be making any sense.

So can it be stated that php.net page is not correct? Would it be correct if it would be saying that these types of characters just stay the same? I am learning the PHP programming and I want to make sure that the info that I have is correct.


Solution

  • It means that a \n (inside double quotes) will be escaped and becomes \\n:

    var_dump("testing\n"); // 8 bytes
    var_dump(addcslashes("testing\n", "\n")); // 9 bytes
    

    Will outputs:

    string(8) "testing
    "
    string(9) "testing\n"