Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backslash + number interpreted by PHP as an octal value, how to block this?

Tags:

php

octal

I have to reformat a string that can contain the following :

$string = "---8\7----";

The problem is that '\7' is interpreted by PHP as an octal value instead of just '\' and then '7'.

How can I block php from doing this please ? Of course, I need to keep the '\' Thanks.

like image 536
SuN Avatar asked Dec 28 '25 16:12

SuN


2 Answers

As others have said, escape the backslash with another backslash, but I feel I should mention why this happen. Your string is written with double quote marks, which in PHP enters into template mode where certain rules gets invoked, and one of them are indeed what you've stumbled upon. Using single quotes fixes this also, by it not doing any templating ;

$string = '---8\7----';

It's the pro's and con's of the two modes, really, but as long as you're aware of this, it shouldn't bite you too hard in the future. :)

like image 70
AlexanderJohannesen Avatar answered Dec 30 '25 06:12

AlexanderJohannesen


You need to give Backslash for escaping that charatcer.

$string = "---8\\7----";
like image 38
Ravichandran Jothi Avatar answered Dec 30 '25 07:12

Ravichandran Jothi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!