while(false !== ($f=readdir($d))){ 的解释

发布于 2024-12-07 10:35:48 字数 544 浏览 0 评论 0原文

我刚刚开始通过《概念、技术和代码》一书练习 php 中的 OOP。不幸的是,我从未在 PHP 中使用过目录和文件,感觉很难理解这种情况,这里是完整的代码

function DirectoryItems($directory){
  $d = "";
  if(is_dir($directory)){
     $d = opendir($directory) or die("Couldn't open directory.");
     while(false !== ($f=readdir($d))){
      if(is_file("$directory/$f")){
      $this->filearray[] = $f;
}
}
closedir($d);
}else{
//error
die("Must pass in a directory.");
}
}

我能理解的是,首先我们检查参数,然后打开该目录,然后读取该目录,然后将目录中的所有文件放入一个数组中,但条件让我困惑到底是什么!==我只知道!=

顺便说一句,这本书是用 PHP4 和 5 编写的

I have just started practicing OOP in php through the book Concepts, Techniques and Codes. Unluckily I have never worked with directories and files in PHP and feeling difficulty to understand this condition here is the full code

function DirectoryItems($directory){
  $d = "";
  if(is_dir($directory)){
     $d = opendir($directory) or die("Couldn't open directory.");
     while(false !== ($f=readdir($d))){
      if(is_file("$directory/$f")){
      $this->filearray[] = $f;
}
}
closedir($d);
}else{
//error
die("Must pass in a directory.");
}
}

All I can understand is first we check the parameter that is it directory after that we open that directory and than we read the directory and putt all the files in the directory into an array but the condition is confusing me what the heck is !== I know only about !=

This book is written in PHP4 and 5 btw

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水水月牙 2024-12-14 10:35:48

!== 与 != 类似,但除了检查相等性之外,它还检查类型。

这是一个重要的区别,因为有时某些东西是“假”或“真”,但实际上并不是具有 false 或 true 值的布尔类型。例如,数字 0 通常被视为 false。

这里第二个稍微令人困惑的部分是代码在 while 循环中检查 false !== (赋值)。这基本上是检查分配的值是否有效。

因此,将所有内容放在一起,代码:

while(false !== ($f=readdir($d))

...翻译为:

$f 被分配了来自 readdir($d) 的对象时,执行...

The !== is like != but in addition to checking the equality it also checks the type.

This is an important distinction because sometimes something is "falsey" or "truthy" but not really a Boolean type with a value of false or true. The number 0 for example is generally treated as false.

The second slightly confusing part here is that the code is checking false !== (assignment) in the while loop. This is basically checking if the assignment was for a valid value.

So to out it all together code:

while(false !== ($f=readdir($d))

...translates to something like:

While $f is assigned an object from readdir($d) do ...

柠栀 2024-12-14 10:35:48

=== 表示“相等的值和相等的类型”
!== 表示“值不相等或类型不相等”,

使用==,空字符串等于 false。但使用 === 时,它们并不相等,因为类型不同。

!=!== 的工作方式相同。额外的 = 符号意味着也应该检查类型,而不仅仅是等效值。

=== means "equal value and equal type"
!== means "not equal value or not equal type"

Using ==, and empty string is equal to false. Using === though, they are not equal because the type is different.

!= and !== work the same way. The extra = sign means that type should be checked too, not just equivalent values.

祁梦 2024-12-14 10:35:48

== 会将值强制为相同类型以进行比较。如果 readdir 返回 0,则 False==0 的计算结果可能为 true。然而,False===0 不会是 true。

有很多人对比较运算符、类型强制、值类型等了解更多。当他们回答时我会删除这些。

The == will coerce values to the same type to compare them. If the readdir returns 0, then False==0 will probably evaluate to be true. However, False===0 will not be true.

There are a lot of people who know a lot more about comparison operators, type coercion, value types, etc. I'll delete this when they answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文