¿Qué son las banderas de PHP en los argumentos de las funciones?

I noticed that some functions in PHP use banderas 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 no?

preguntado el 09 de marzo de 12 a las 14:03

Sólo var_dump() the cosntant, it'll show you the content. -

2 Respuestas

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

Mira los ejemplos para json_encode().

As you can see, each flag is 2n. De esta manera, | se puede utilizar para especificar multiple flags.

For example, suppose you want to use the flag JSON_FORCE_OBJECT (16 or 00010000) y 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"

Teclado de códigos.

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

respondido 09 mar '12, 14:03

Also, these are called "bit flags". - PernoReloj

Usually flags are integers that are consecutive powers of 2, so that each has one bit set to 1 and all others to 0. This way you can pass many binary values in a single integer using bit-wise operators. See this for more (and probably more accurate) información.

respondido 09 mar '12, 14:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.