函数参数中的PHP标志是什么? [英] What are PHP flags in function arguments?

查看:98
本文介绍了函数参数中的PHP标志是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到PHP中的一些函数使用 flags 作为参数。是什么使它们唯一而不是纯字符串参数?我在问,因为我想在我自己的自定义函数上使用它们,但很好奇这个过程是什么。



编辑:

解决方案

它们只是映射到数字的常量,例如 SORT_NUMERIC (排序函数使用的常量)是整数 1



查看 json_encode()



正如你所看到的,每个标志都是2 n 。这样, | 可以用来指定多个标志 例如,假设你想使用标志 JSON_FORCE_OBJECT 16 00010000 )和 JSON_PRETTY_PRINT 128 10000000 )。



如果任一操作数位打开,则按位运算符OR( | )将打开位。 p>

  JSON_FORCE_OBJECT | JSON_PRETTY_PRINT 

...在内部....

  00010000 | 1000000 

...这是...

  10010000 

您可以使用...

  var_dump(base_convert(JSON_PRETTY_PRINT | JSON_FORCE_OBJECT,10,2)); 
// string(8)10010000

CodePad



这两个标记可以如何设置 / em>与位运算符。


I noticed that some functions in PHP use flags as arguments. What makes them unique instead of plain string arguments? I'm asking since I want to use them on my own custom functions but am curious as to what the process is for doing so.

Edit: TO summarize, when is it best to create a custom function with flags and when is it not?

解决方案

They are just constants which map to a number, e.g. SORT_NUMERIC (a constant used by sorting functions) is the integer 1.

Check out the examples for json_encode().

As you can see, each flag is 2n. This way, | can be used to specify multiple flags.

For example, suppose you want to use the flag JSON_FORCE_OBJECT (16 or 00010000) and JSON_PRETTY_PRINT (128 or 10000000).

The bitwise operator OR (|) will turn the bit on if either operand's bit is on...

JSON_FORCE_OBJECT | JSON_PRETTY_PRINT

...is internally....

00010000 | 1000000

...which is...

10010000

You can check it with...

var_dump(base_convert(JSON_PRETTY_PRINT | JSON_FORCE_OBJECT, 10, 2));
// string(8) "10010000"

CodePad.

This is how both flags can be set with bitwise operators.

这篇关于函数参数中的PHP标志是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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