以 UTF-8 格式保存所有源代码文件有什么缺点吗?
如果相关的话(很可能是),它们是 PHP 源代码文件。
If that's relevant (it very well could be), they are PHP source code files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如果相关的话(很可能是),它们是 PHP 源代码文件。
If that's relevant (it very well could be), they are PHP source code files.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
有一些陷阱需要注意:
不会触发警告,那就没问题。
strlen
真正返回字符串中的字节数,而不是实际的字符数。在您开始使用substr
之类的函数拼接非 ASCII 字符的字符串之前,这并不是什么大问题:当您这样做时,传递给它的索引引用字节索引而不是字符索引,并且这可能会导致您的脚本将非 ASCII 字符分成两部分。例如,echo substr("é", 0, 1)
将返回无效的 UTF-8 字符,因为在 UTF-8 中,é
实际上占用两个字节,而 substr 将返回一个无效的 UTF-8 字符。仅返回第一个。 (解决方案是使用mb_
字符串函数 ,它们支持多字节编码。)SET CHARACTER SET UTF8
或类似的内容),或者,如果您找不到更好的方法,mb_convert_encoding
或iconv
会将一个字符串转换为另一种编码。There are a few pitfalls to take care of:
<?php header('Content-Type: text/html') ?>
at the beginning of an otherwise empty file doesn't trigger a warning, you're fine.strlen
really returns the number of bytes in the string, not the actual number of characters. This isn't too much of a problem until you start splicing strings of non-ASCII characters with functions likesubstr
: when you do, indices you pass to it refer to byte indices rather than character indices, and this can cause your script to break non-ASCII characters in two. For instance,echo substr("é", 0, 1)
will return an invalid UTF-8 character because in UTF-8,é
actually takes two bytes and substr will return only the first one. (The solution is to use themb_
string functions, which are aware of multibyte encodings.)SET CHARACTER SET UTF8
or something along these lines), or if you couldn't find a better way,mb_convert_encoding
oriconv
will convert one string into another encoding.实际上,通常建议您将所有源保留为 UTF8。带有拉丁字符的常规代码的大小根本不重要,但可以防止任何特殊字符出现故障。
It's actually usually recommended that you keep all sources in UTF8. It won't matter size of regular code with latin characters at all, but will prevent glitches with any special characters.
如果您在字符串值等中使用任何特殊字符,则大小会稍大一些,但这并不重要。
尽管如此,我的建议是始终保留默认格式。我花了很多时间,因为格式保存出错,所有字符都改变了。
从技术角度来看,没有什么区别!
If you are using any special chars in e.g string values, the size is a little bit bigger, but that shouldn't matter.
Nevertheless my suggestion is, to always leave the default format. I spent so many hours because there was an error with the format saving and all characters changed.
From a technical point of few, there isn't a difference!
非常相关的是,PHP 解析器可能会开始输出虚假字符,例如一个时髦的倒置问号。只需遵守规范即可,这是首选。
Very relevant, the PHP parser may start to output spurious characters, like a funky unside-down questionmark. Just stick to the norm, much preferred.