Binary Data (Boolean) In PHP

Lecture



This is the simplest type. It expresses the truth of a value — it can be either TRUE or FALSE . Boolean type was introduced in PHP4.

To define a boolean type, use the keyword TRUE or FALSE . Both are case-independent.

<?php
$x = True; // присвоить $x значение TRUE
?>

A certain operator is usually used, which returns a logical expression and then transmits it to the control structure.

<?php
// == это оператор, который проверяет
// эквивалентность и возвращает булево значение
if ($action == "показать_версию") {
echo "Версия 1.23";
}

// это не обязательно...
if ($show_separators == TRUE) {
echo "<hr> ";
}

// ...потому что вы можете просто написать
if ($show_separators) {
echo "<hr> ";
}
?>

Conversion to Boolean

To convert a value to a boolean type, use a cast (bool) or (boolean) cast. However, in most cases you do not need to use a cast, since the value will be automatically converted if the operator, function, or control construct requires a boolean argument.

When converting to a boolean type, the following values ​​are considered FALSE :

  • Boolean itself FALSE

  • integer 0 (zero)

  • floating point number 0.0 (zero)

  • empty line and string "0"

  • array with zero elements

  • object with zero member variables

  • special type NULL (including unset variables)

All other values ​​are treated as TRUE (including any resource).

Attention! -1 is considered TRUE , like any non-zero (negative or positive) number!

<?php
var_dump((bool) ""); // bool(false)
var_dump((bool) 1); // bool(true)
var_dump((bool) -2); // bool(true)
var_dump((bool) "foo"); // bool(true)
var_dump((bool) 2.3e5); // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array()); // bool(false)
var_dump((bool) "false"); // bool(true)
?>

Work with binary data (type boolean)

Strings may contain any, including binary data. To work with such strings sometimes
convenient to use the functions pack () and unpack () .

pack (string $ format [, mixed $ args, ...])

The pack () function wraps the specified arguments into a binary string, which is then returned. The format of the parameters, as well as their number, is specified using the $ format string, which is a set of single-letter formatting specifiers - like those specified in sprintf () , but only without the % sign. After each qualifier there can be a number that indicates how much information will be processed by this specifier. Namely, for the a, A, h and H formats, the number specifies how many characters will be placed in the binary string from those that are in the next line parameter when the function is called (that is, the size of the field for outputting the string is determined). In the case of @, it determines the absolute position in which the following data will be placed. For all other qualifiers, the numbers following them specify the number of arguments to which this format applies. Instead of a number, you can specify *, in this case it is assumed that the specifier acts on all the remaining data. Here is a complete list of format specifiers:

a - string, empty spaces in the field are filled with a symbol with the code 0;
A - string, empty spaces are filled with spaces;
h is a hexadecimal string, lower-order bits at the beginning;
H is a hexadecimal string, the leading bits are at the beginning;
c - sign byte (character);
C - unsigned byte;
s - signed short integer (16 bits, the byte order is determined by the architecture
processor swarm);
S is an unsigned short integer;
n - unsigned integer (16 bits, high order at the end);
v - unsigned integer (16 bits, low-order bits at the end);
i is a signed integer (the size and order of bytes is determined by the architecture);
I is an unsigned integer;
l - signed long integer (32 bits, the byte order is determined by the architecture
Roy);
L is an unsigned long integer;
N - unsigned long integer (32 bits, high order bits at the end);
V - unsigned integer (32 bits, low-order bits at the end);
f is a floating point number (depending on the architecture);
d is a double-precision floating-point number (depending on the architecture);
x is a character with a zero code;
X - return back by 1 byte;
@ - filling the zero code to the specified absolute position.

A lot, is not it? Here is an example of using this feature:

// Целое, целое, все остальное — символы
$bindata = pack("nvc*", 0x1234, 0x5678, 65, 66);

After executing the above code, the $ bindata line will contain 6 bytes in the following sequence: 0x12, 0x34, 0x78, 0x56, 0x41, 0x42 (in hexadecimal notation).

unpack (string $ format, string $ data)

The unpack () function performs the inverse actions. Pack () - unpacks the $ data line using the $ format format information . It returns an associative array containing the elements of the unpacked data. The $ format string is set in a slightly different format than in the pack () function, namely, after each qualifier (or after the terminating number), the key name in the associative array should follow. Parameters are separated using the / symbol. Example:

$array=unpack("c2chars/nint", $bindata);

Elements with keys will be written to the resulting array: chars1 , chars2 and int . As you can see, if a number is specified after the specifier, then the numbers 1, 2, etc. will be added to the key name, i.e., several keys will appear in the array, differing in suffixes.
When are pack () and unpack () useful? For example, you considered a section of a GIF file containing its size in pixels, and you want to convert a binary 32-bit memory cell into a format understandable by PHP. Or, on the contrary, strive to work with files with a fixed record size. In this case, the functions in question will be useful to you. Generally speaking, the pack () and unpack () functions are used relatively rarely. This is due to the fact that in PHP almost all actions that may require working with binary data (for example, analyzing a picture file to determine its size) have already been implemented as built-in functions, for example, with a GIF image it is GetImageSize () .

created: 2016-01-25
updated: 2021-03-13
132445



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)