Home  >  Article  >  Backend Development  >  php-这个函数中按位异或操作的作用?

php-这个函数中按位异或操作的作用?

WBOY
WBOYOriginal
2016-06-02 11:33:431018browse

php

<code> function file_mode_info($file_path){    /* 如果不存在,则不可读、不可写、不可改 */    if (!file_exists($file_path))    {        return false;    }    $mark = 0;    if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')    {        /* 测试文件 */        $test_file = $file_path . '/cf_test.txt';        /* 如果是目录 */        if (is_dir($file_path))        {            /* 检查目录是否可读 */            $dir = @opendir($file_path);            if ($dir === false)            {                return $mark; //如果目录打开失败,直接返回目录不可修改、不可写、不可读            }            if (@readdir($dir) !== false)            {                $mark ^= 1; //目录可读 001,目录不可读 000            }            @closedir($dir);            /* 检查目录是否可写 */            $fp = @fopen($test_file, 'wb');            if ($fp === false)            {                return $mark; //如果目录中的文件创建失败,返回不可写。            }            if (@fwrite($fp, 'directory access testing.') !== false)            {                $mark ^= 2; //目录可写可读011,目录可写不可读 010            }            @fclose($fp);            @unlink($test_file);            /* 检查目录是否可修改 */            $fp = @fopen($test_file, 'ab+');            if ($fp === false)            {                return $mark;            }            if (@fwrite($fp, "modify test.\r\n") !== false)            {                $mark ^= 4;            }            @fclose($fp);            /* 检查目录下是否有执行rename()函数的权限 */            if (@rename($test_file, $test_file) !== false)            {                $mark ^= 8;            }            @unlink($test_file);        }        /* 如果是文件 */        elseif (is_file($file_path))        {            /* 以读方式打开 */            $fp = @fopen($file_path, 'rb');            if ($fp)            {                $mark ^= 1; //可读 001            }            @fclose($fp);            /* 试着修改文件 */            $fp = @fopen($file_path, 'ab+');            if ($fp && @fwrite($fp, '') !== false)            {                $mark ^= 6; //可修改可写可读 111,不可修改可写可读011...            }            @fclose($fp);            /* 检查目录下是否有执行rename()函数的权限 */            if (@rename($test_file, $test_file) !== false)            {                $mark ^= 8;            }        }    }    else    {        if (@is_readable($file_path))        {            $mark ^= 1;        }        if (@is_writable($file_path))        {            $mark ^= 14;        }    }    return $mark;}</code>

$mark ^= 1;
$mark ^= 2;
$mark ^= 4;
$mark ^= 8;
等等这几个操作的作用是什么? 为什么要这么做?
而且里面的判断都用 !== (非全等),这么做的作用是什么?
为什么不用普通的不等于 != ?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn