简体   繁体   English

访问定义的常量将返回混乱的字符

[英]Accessing a defined constant returns messed up characters

This is strange I have a constant defined as such: 奇怪的是我有一个这样定义的常量:

define("RJ_FILES_PATH", RJ_SITE_DIRECTORY."\assets\files\\");

However when I try to access the constant in code I get this weird result on my localhost.. 但是,当我尝试访问代码中的常量时,在本地主机上却得到了这个奇怪的结果。

C:\wamp\www\my.app\assetsiles\2688

The \\f is replaced by a square indicating it as an unrecognised character. \\f替换为一个正方形,表示它是无法识别的字符。 Whats happening because of this I am unable to save files to the folder the constant is supposed to point to. 因此,发生了什么事,我无法将文件保存到常量应该指向的文件夹中。

You need to escape the backslashes before the a and the f : 您需要在af之前转义反斜杠:

define("RJ_FILES_PATH", RJ_SITE_DIRECTORY."\\assets\\files\\");

Otherwise they're interpreted as escape codes, since they're within a string. 否则,它们被解释为转义码,因为它们在字符串中。 (Just like \\n is a newline, et cetera.) (就像\\n是换行符,等等)。

You could—and probably should —just use forward slashes ( / ) in your file/directory paths. 您可以并且可能应该仅在文件/目录路径中使用正斜杠( / )。 PHP will automatically convert them to the value of the built-in system-dependent constant DIRECTORY_SEPARATOR when using the string as a file path. 当使用字符串作为文件路径时,PHP会将它们自动转换为与系统相关的内置常数DIRECTORY_SEPARATOR的值。 This is by far the most cross-platform method of doing it. 这是迄今为止最跨平台的方法。

Alternatively, you could use single quotes. 或者,您可以使用单引号。 They interpolate backslashes ( \\ ) differently in that most escapes are ignored and just interpreted literally (the exceptions being \\\\ and \\' ). 它们对反斜杠( \\ )进行插值的方式有所不同,因为大多数转义都被忽略,只是按字面意义进行解释(例外为\\\\\\' )。

#                                                       *
define('RJ_FILES_PATH', RJ_SITE_DIRECTORY.'\assets\files\\');
# * still need an escape here because of \'

You should escape the backlash by double it: \\ In my opinion, you always should use '/', because it work fine in windows and linux. 您应该通过将其倍增来避免反冲:\\我认为,您始终应该使用'/',因为它在Windows和Linux中可以正常工作。 In php, there's a constant DIRECTORY_SEPARATOR but it's uneccesary because '/' work fine. 在php中,有一个常量DIRECTORY_SEPARATOR,但这是不必要的,因为'/'工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM