为什么 PHP 在处理二进制数据的函数上没有长度参数?
为什么处理二进制数据的函数没有长度参数?我来自 C 语言背景,所以这真的让我很困惑。我认为字符串对象可能保存长度,但事实似乎并非如此(如果我错了,请纠正我)。表现出这种行为的两个函数示例是 base64_encode/decode 和 bin2hex。
谢谢!
Why is it that functions that deal with binary data dont have length arguments? I come from a C background, so this is really confusing me. I thought that perhaps the string object holds the length, but that doesnt appear to be the case (correct me if im wrong). Two examples of functions that exhibit this behavior are base64_encode/decode and bin2hex.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
php 的字符串对象在内部保存长度。你可以通过调用
strlen
来获取这个值,但是由于php有自动管理,你很少会需要担心的是:php's string objects hold the length internally. You can get this value by calling
strlen
, but since php has automatic management, you rarely need to worry about that:PHP 字符串与 C 字符串不同。
自从我思考内部结构以来已经有一段时间了。我记得,php 变量被称为“zvals”,它基本上是一个跟踪数据类型、长度和数据本身等内容的结构。
因此,在内部(至少在相当低的水平上),PHP 可能会避免依赖空终止来了解字符串的结束位置。
编辑:这是一篇旧文章,讨论了 PHP 中用户空间变量的一些内部实现
PHP strings are not like C-strings.
It's been a while since I thought about the internals. As I remember, php variables are known as "zvals", which is basically a struct that tracks things like the datatype, length, and the data themselves.
So internally (at least down to a pretty low level), PHP probably avoids relying on null-termination to know where a string ends.
EDIT: Here's an old article that discusses some of the internal implementation of userspace variables in PHP