search
Homephp教程php手册浅析PHP 按位与或 (^ 、&)

浅析PHP 按位与或 (^ 、&)

Jun 13, 2016 am 11:46 AM
ampphpDownandWorkhavefriendofquestion

今天朋友群里朋友问了下 按位与或的问题。。
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
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)