Welcome Guest, Not a member yet? Register   Sign In
View string needs escape if back slash on end
#1

(This post was last modified: 06-28-2019, 09:55 AM by InsiteFX. Edit Reason: updated )

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#2

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.
Reply
#3

Ok, Thanks Lonnie I was just checking on it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

You need it with both - always looks awkward but the only way to avoid escaping it.
Reply
#5

I ended up solving it this way and it works fine.

PHP Code:
echo view('Insitefx\Admin\Views'.DIRECTORY_SEPARATOR.$slug); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(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....
Reply
#7

You know I think you are right because this is a Windows 10 Pro x64 system.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#8

So it would be best just to escape it Lonnie?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9

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 = '\\';
Reply
#10

(This post was last modified: 07-03-2019, 06:49 AM by dave friend.)

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB