什么是使用单管'|'在函数参数做什么? [英] What Does Using A Single Pipe '|' In A Function Argument Do?

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

问题描述

就拿下面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.


  • 正好与函数中的参数值发生了什么?

  • 我可以使用同样的事情在我自己的职能?

  • 是因此如​​何,以及是否有利于本在说传递一个数组呢?

感谢您提前。

推荐答案

按位运算符修改所涉及的值的位。按位基本上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重presentation而这正是你会得到什么,如果你:

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

echo 5 | 2;



正如伊格纳西奥指出,这是最经常在PHP(和其他汉语语言)所使用的一种方式多个标志结合。每个标志通常被定义为,其值通常设置为重新presents仅有一个位在一个整数常数的不同的偏移

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

然后,当你这两人一起操作每个对自己的位偏移,绝不会发生冲突:

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

FLAG_A | FLAG_C

语句:

1 | 100

所以,你最终开启:

So you end up turning on:

101

这将重新presents整数5。

Which represents the integer 5.

然后所有的code的做—以下code将反应被设置&MDASH不同的标志;在下面的(使用按位

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天全站免登陆