什么是& & 2在PHP中意味着什么? [英] What does & in &2 mean in PHP?
问题描述
在以下代码的最后一行中,它具有&2, if($page['special']&2).
In the last line of the following code, it has &2, if($page['special']&2).
&是什么是什么意思?
What does & mean?
if(isset($_REQUEST['id']))$id=(int)$_REQUEST['id'];
else $id=0;
if($id){ // check that page exists
$page=dbRow("SELECT * FROM pages WHERE id=$id");
if($page!==false){
$page_vars=json_decode($page['vars'],true);
$edit=true;
}
}
if(!isset($edit)){
$parent=isset($_REQUEST['parent'])?(int)$_REQUEST['parent']:0;
$special=0;
if(isset($_REQUEST['hidden']))$special+=2;
$page=array('parent'=>$parent,'type'=>'0','body'=>'','name'=>'','title'=>'','ord'=>0,'description'=>'','id'=>0,'keywords'=>'','special'=>$special,'template'=>'');
$page_vars=array();
$id=0;
$edit=false;
}
// { if page is hidden from navigation, show a message saying that
if($page['special']&2)echo '<em>NOTE: this page is currently hidden from the front-end navigation. Use the "Advanced Options" to un-hide it.</em>';
推荐答案
$page['special'] & 2
表示
$page['special']
按位与2
>
基本上检查是否在$page['special']
中设置了2位.
It basically checks to see if the 2 bit is set in $page['special']
.
此行:
if(isset($_REQUEST['hidden']))$special+=2;
明确地将$special
加2,以便满足按位AND运算和比较,因为十进制2 ==二进制10,其中1代表2 1 位,确保它是设置.
explicitly adds 2 to $special
so that it'll satisfy the bitwise AND operation and comparison, because decimal 2 == binary 10, with the 1 representing the 21 bit, ensuring it is set.
如果设置了2位,则AND操作返回2,在PHP中解析为true
并且满足条件;否则返回0,该值被视为false
.
The AND operation returns 2 if the 2 bit is set, which resolves to true
in PHP and satisfies the condition; otherwise it returns 0 which is considered false
.
IMO相当巧妙,虽然不确定它的安全性.
Quite a neat trick IMO, not sure how secure it is though.
这篇关于什么是& & 2在PHP中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!