CodeIgniter Forums

Full Version: View string needs escape if back slash on end
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Not sure if this is a bug or not but while working on my adminLTE 3 module
when I have an echo view like this:

PHP Code:
echo view('Insitefx\Admin\Views\'.$slug); 

All code lines after the view are red with error: (Expecting right single quote or double quote).
As you can see it is quoted correctly, if I escape the back slash like this:

PHP Code:
echo view('Insitefx\Admin\Views\\'.$slug); 

Then everything is alright and it will load the Admin module view with no errors.

It also works correctly using just single forward slashes.

But should it also work with single back slashes? This is a Windows 10 Pro x64-bit System.
Pretty sure that's just PHP being PHP. With a backslash just before the closing single quote, the backslash is trying to be an escape character. Though it's possible it works just fine in PHP and your editor is interpreting it incorrectly. I know you need it with double-quotes, not sure with singles.
Ok, Thanks Lonnie I was just checking on it.
You need it with both - always looks awkward but the only way to avoid escaping it.
I ended up solving it this way and it works fine.

PHP Code:
echo view('Insitefx\Admin\Views'.DIRECTORY_SEPARATOR.$slug); 
(06-29-2019, 08:55 AM)InsiteFX Wrote: [ -> ]I ended up solving it this way and it works fine.

PHP Code:
echo view('Insitefx\Admin\Views'.DIRECTORY_SEPARATOR.$slug); 

I think that will only work on Windows servers, no? Directory separator on *nix boxes is a forward slash....
You know I think you are right because this is a Windows 10 Pro x64 system.
So it would be best just to escape it Lonnie?
Escaping it is a fine solution. There's really no other way to deal with a trailing backslash unless you want to define your own constant or variable, but then you'll still need to escape it:

$namespaceDivider = '\\';
This reference implies that forward slashes should not be an issue which, if true, should allow use of DIRECTORY_SEPARATOR.

On the other hand, there is this bit of advice.
Pages: 1 2