PHP escape character in strings

I would like to be able to type strings like so in PHP:
$route = “{controller}/{action}/{id}”;

And like so:
$route = “\{controller\}/{action}/{id}”;

Hence the escape character “\”.

What annoys me however is that if I want to do the following:
$route = “\{controller\}/{action}/{id}”;

PHP automatically converts this to:
$route = “{controller}/{action}/{id}”;

Despite the { and } symbols having no special meaning in PHP.

I would like to be able to stop PHP from parsing the backslash as an escape character for characters that have no special meaning in PHP when escaped, so that when I do the following:
$route = “\{controller\}/{action}/{id}”;

$route actually contains the following value:

This is because I am trying to achieve a clean and consistent API, that reflects the way in which PHP works, but at the moment in order to do so I have to type the following:
$route = “\\{controller\\}/{action}/{id}”;

To basically say that the { and } symbols have been escaped.

I’m not sure submitting this issue as a bug report to PHP will get me anywhere, and as far as I’m aware there is little I can do to prevent this behaviour in PHP. Has anyone got any suggestions that might be of benefit?

I’m aware that some might say that this is the desired behaviour in PHP, but I don’t see myself why PHP would escape characters that have no special meaning. I can understand
being turned into a newline, but \{ being turned into { and \} being turned into } just doesn’t make sense, unless this is done for consistency, which I find annoying as I’d like to be able to replicate the PHP ability of escaping characters with a backslash like PHP does in the case of \" and \’ but for my own set of characters.

Are you actually entering these as in the above, i.e. as a string definition within your code? That is, can’t you just surround with single-quotes and not have the escape characters interpreted at all? (I’m new to this, so please ignore if that’s just missing the point of the question).

Nope, I had the same question, you just beat me to it :slight_smile: I’m curious to the answer as well.

@Bonner ; Can you explain your situation a bit more? As I’m confused on when you’d run into the scenarios you are trying to illustrate.

The value of the $route variable is a string. The format of the string doesn’t really matter other than the following characters have special meanings, but are valid characters within a URL too:
{ = opening brace for segment
} = closing brace for segment

In a URL, these characters are valid, and because I am working on a framework there is no way of knowing if or how these characters will be used so it is important that these characters can be escaped.

From my point of view PHP should provide a way to allow raw strings without any conversion (which is what most would expect from using single quotes with the exception of using \’ to escape single quotes), so that us as developers can deal with the string how we see fit, or at least change the way in which the escaping works so that \\ does not convert to \ when using single quotes.

To put it even more simple:
$route = ’
'; // $route now contains the value \

$route = '\
'; // $route now contains the value \

If the
isn’t transformed into a newline, why is \\ transformed to a \

I personally think that if using single quotes, the value \\ should be left as \\

That is probably the simplest I can put it.

I’m not sure why I used double quotes in the first example, when it would have been better to demonstrate this issue using single quotes.

When using double quotes, the following should happen (which is the case):
$route = "
"; // $route now contains a newline character
$route = "\
"; // $route now contains the value \

When using single quotes, the following should happen in my opinion:
$route = ’
'; // $route should now contain the value \

$route = '\
'; // $route should now contain the value \\

But as in the first example it doesn’t.

I’m sure there is good reason for it (probably because most other languages do this as well), but you can read more about how the escape character (\) is utilized in PHP

I personally think it wasn’t well thought through, on the basis of looking at other languages. If you look at C# fas it closely resembles PHP in terms of syntax, the language doesn’t suffer the same problem.

In C#, to escape a character with a special meaning you repeat the character like so:
route = “{controller}/{action}/”“this is still part of the route”“/{id}”

So in C#, I could do the following:
route = “{{controller}}/{action}”

Which would result in route containing the value

And this would then allow me to pass the value to the string parser and the string parser would know that {{controller}} is not a segment but rather part of the URL, and converts it to the string {controller} opposed to parsing it as a segment.

My gripe is the consistency issues with not being able to just do \{ as I have to do \\{ in order to achieve what I want which is inconsistent with the way in which PHP’s own special characters are escaped.

I can imagine the only reason PHP has done this is to allow for single quotes to be escaped like so \’ and if you don’t want the escaping to happen you can do \\’ and because of this \ can’t be used to escape other characters when using strings surrounded by single quotes.

So I’m in two minds whether to go with the \\{ for escaping or {{ and I suppose this is a matter of preference when it comes to the whole PHP community. I guess most would probably go with the \\{ but I can’t make an affirmative judgement since most MVC frameworks in PHP don’t even allow for the { and } characters to be escaped.