浅析PHP 按位与或 (^ 、&)
Jun 13, 2016 am 11:46 AM
amp
php
。
Down
and
Work
have
friend
of
question
今天朋友群里朋友问了下 按位与或的问题。。
PHP 工作有1年了 可能对这块不太熟悉吧 在这里给新手朋友介绍下这部分
按位于主要是对二进制数操作。
复制代码 代码如下:
$a = 1;
$b = 2;
$c = $a^b;
echo $c // 3
?>
这里不是单纯的相加关系
十进制 1换算成二进制 00000001
十进制 2换算成二进制 00000010
按位^ 00000011 // 就是不相同的都算成1 啦 ^_^
然后,
复制代码 代码如下:
$a = 1;
$b = 2;
echo $a & $c; // 1
?>
十进制 3换算成二进制 00000011
十进制 1换算成二进制 00000001
按位& 00000001 // 就是各个位数相同的不变否则都算成0
最后介绍下用法 ; 按位&后返回值是没意义的。 主要是用来判断$a 是否存在于 $c // 权限用法比较多。
复制代码 代码如下:
$my_privilege = 15; // 1+2+4+8 拥有全部权限
$Pri = '';
$privilege_arr = array(8=>'增', 4=>'删',2=>'改',1=>'查');
foreach($privilege_arr as $k =>$v){
$k & $my_privilege && $Pri .= '我有'.$v.'的权力
';
}
echo $Pri;
?>
Statement of this Website
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

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
