使用单个管道 '|' 有何作用在一个函数参数中做什么? [英] What Does Using A Single Pipe '|' In A Function Argument Do?

查看:21
本文介绍了使用单个管道 '|' 有何作用在一个函数参数中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下面的代码为例:

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.

  • 函数中的参数值究竟发生了什么?
  • 我可以在自己的函数中使用相同的东西吗?
  • 是这样吗?与说传递数组相比,这样做有什么好处吗?

推荐答案

位运算符

按位运算符修改所涉及值的位.按位 OR 基本上将左右参数的每一位或在一起.例如:

Bitwise operators

Bitwise operators modify the bits of the values involved. A bitwise OR basically ORs together each bit of both the left and right argument. For example:

5 | 2

将转换为位/二进制为:

Would translate to bits/binary as:

101 | 10

这将导致:

111

因为:

1 || 0 = 1
0 || 1 = 1
1 || 0 = 1

作为一个代表 7 的整数,这正是你得到的:

And as an Integer that is the representation of 7 which is exactly what you get if you:

echo 5 | 2;


用埃迪·伊扎德的话来说......旗帜!

正如 Ignacio 所说,这在 PHP(和其他语言)中最常用作组合多个标志的方式.每个标志通常定义为一个常量,其值通常设置为一个整数,该整数仅表示不同偏移量的一位:


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:

define('FLAG_A', 1); /// 0001
define('FLAG_B', 2); /// 0010
define('FLAG_C', 4); /// 0100
define('FLAG_D', 8); /// 1000

然后,当您将这些 OR 放在一起时,它们会根据自己的位偏移进行操作,并且永远不会发生冲突:

Then when you OR these together they operate each on their own bit offset and will never collide:

FLAG_A | FLAG_C

翻译成:

1 | 100

所以你最终打开了:

101

代表整数5.

然后所有代码必须做的——对设置的不同标志做出反应的代码——如下(使用按位AND):

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):

$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.

这篇关于使用单个管道 '|' 有何作用在一个函数参数中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆