In PHP do i need to escape backslashes?

后端 未结 6 1045
予麋鹿
予麋鹿 2020-11-30 10:09

I think this may be a stupid question, but I am quite confused whether I need to escape backslash in PHP.

echo \'Application\\Models\\User\'; prints Applicat         


        
6条回答
  •  青春惊慌失措
    2020-11-30 10:31

    I think it depends on the context, but it is a good idea to escape backslashes if using it in file paths.

    Another good idea is to assign the directory separator to a constant, which I've seen done in various applications before, and use it as thus:

    
    

    If you wish to save space and typing, others use DS for the constant name.

    
    

    This keeps your application portable if you move from a Windows environment to a *nix environment, as you can simple change the directory separator constant to a forward slash.

提交回复
热议问题