php 使用单个管道 '|' 有何作用 在一个函数参数中做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13811922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What Does Using A Single Pipe '|' In A Function Argument Do?
提问by cryptic ツ
Take for instance the following code:
以下面的代码为例:
phpinfo(INFO_MODULES | INFO_ENVIRONMENT | INFO_VARIABLES);
A single argument is being used, but I am providing a list of options separated by a single pipe symbol.
正在使用单个参数,但我提供了由单个管道符号分隔的选项列表。
- What exactly is happening with the argument value in the function?
- Can I use the same thing in my own functions?
- Is so how, and are there benefits to this over say passing an array instead?
- 函数中的参数值究竟发生了什么?
- 我可以在自己的函数中使用相同的东西吗?
- 是这样吗,而不是说传递数组有什么好处?
回答by Pebbl
Bitwise operators
按位运算符
Bitwise operators modify the bits of the values involved. A bitwise ORbasically ORs together each bit of both the left and right argument. For example:
按位运算符修改所涉及值的位。按位OR基本上将左右参数的每一位进行或运算。例如:
5 | 2
Would translate to bits/binary as:
将转换为位/二进制为:
101 | 10
Which would result in:
这将导致:
111
Because:
因为:
1 || 0 = 1
0 || 1 = 1
1 || 0 = 1
And as an Integer that is the representation of 7 which is exactly what you get if you:
作为一个代表 7 的整数,这正是你得到的结果,如果你:
echo 5 | 2;
In the words of Eddie Izzard... Flag!
用埃迪·伊扎德的话来说......旗帜!
As Ignacio states, this is most often used in PHP (and other langauges) as a way to combine multiple flags. Each flag is usually defined as a constant whose value is normally set to an integer that represents just one bit at a different offset:
正如 Ignacio 所说,这在 PHP(和其他语言)中最常用作组合多个标志的方式。每个标志通常被定义为一个常量,其值通常设置为一个整数,仅表示不同偏移量的一位:
define('FLAG_A', 1); /// 0001
define('FLAG_B', 2); /// 0010
define('FLAG_C', 4); /// 0100
define('FLAG_D', 8); /// 1000
Then when you ORthese together they operate each on their own bit offset and will never collide:
然后,当您将OR它们放在一起时,它们会根据自己的位偏移进行操作,并且永远不会发生冲突:
FLAG_A | FLAG_C
Translates to:
翻译成:
1 | 100
So you end up turning on:
所以你最终打开:
101
Which represents the integer 5.
代表整数5。
Then all the code has to do—the code that will be reacting to the different flags being set—is the following (using a bitwise AND):
然后所有必须做的代码——对设置的不同标志做出反应的代码——如下(使用按位AND):
$combined_flags = FLAG_A | FLAG_C;
if ( $combined_flags & FLAG_A ) {
/// do something when FLAG_A is set
}
if ( $combined_flags & FLAG_B ) {
/// this wont be reached with the current value of $combined_flags
}
if ( $combined_flags & FLAG_C ) {
/// do something when FLAG_C is set
}
At the end of the day it just makes things easier to read by having named constants, and generally more optimal by relying on integer values rather than strings or arrays. Another benefit of using constants is that if they are ever mistyped when used, the compiler is in a better situation to tell and to throw a warning... if a string value is used it has no way of knowing that anything is wrong.
在一天结束时,它只是通过命名常量使事情更容易阅读,并且通常通过依赖整数值而不是字符串或数组来优化。使用常量的另一个好处是,如果它们在使用时被打错了,编译器会更好地告诉并抛出警告……如果使用字符串值,它无法知道有什么问题。
define('MY_FLAG_WITH_EASY_TYPO', 1);
my_function_that_expects_a_flag( MY_FLAG_WITH_EASY_TPYO );
/// if you have strict errors on the above will trigger an error
my_function_that_expects_a_flag( 'my_string_with_easy_tpyo' );
/// the above is just a string, the compiler knows nowt with
/// regard to it's correctness, so instead you'd have to
/// code your own checks.
回答by Ignacio Vazquez-Abrams
You're passing an argument which is the bitwise OR of multiple flags. You can use the operator anywhere you like.
您正在传递一个参数,该参数是多个标志的按位或。您可以在任何您喜欢的地方使用该运算符。

