PHP != and == operators
This has boggled me for a while. I am running through a directory and echo'ing out its contents, and I want to exclude the ".." and "." files.
Now, this code works:
if ($files = scandir("temp/"))
{
foreach ($files as $file)
{
if ($file == ".." OR $file == ".")
{
}
else {
echo $file;
echo "<br>";
}
}
}
But this doesn't...
if ($files = scandir("temp/"))
{
foreach ($files as $file)
{
if ($file != ".." OR $file != ".")
{
echo $file;
echo "<br>";
}
}
}
For obvious reasons the second lump of code is more what I want, because I really hate having the true statement do nothing.
If you negate a condition consisting of two single conditions and a conjunction ("and" or "or"), you need to negate each condition separately and use the other conjunction.
So try this instead:
if ($file != ".." AND $file != ".")
This is one of deMorgan's Laws.
not (A OR B) = (not A) AND (not B)
The change you are making is a refactoring called Reverse Conditional
They're not opposites...
Check out de Morgan's laws.
if($file != ".." OR $file != ".")
should be
if($file != ".." AND $file != ".")
链接地址: http://www.djcxy.com/p/1794.html
上一篇: 杂耍和(严格)大/小
下一篇: PHP!=和==运算符