Mr. Developer Mr. Developer - 1 year ago 85
PHP Question

How to add backslash next to php magic constants?

I'm trying to check if file exists or not. I tried simple http URL for the task but

does not support (my php version is
allow url fopen
is activated ).

So i tried in different way and its working, see below

if(file_exists(__DIR__.'\email_template.php')) {
echo 'Template is available.';

Problem is that i'm adding template name dynamically and i need backslash between
but i can't concatenate properly. I tried below

$__DIR = __DIR__.'\';
$__DIR = __DIR__.'"\"';
$__DIR = __DIR__."\";

But no one is working, its return syntac error. So can anyone guide me how can i fix the issue i would like to appreciate. Thank you

Answer Source

Try this:

$__DIR = __DIR__.'\\';

Backshlash is special char in PHP (and other languages as well), that is used for noting that chars after them should be interpreted in special way - and is named "escape char". You do not want this to happen, so You should escape backshlas... By using one more backshlash. ;)

You can read more here.