How to get endianness type in PHP?

In C #, I can get the endianness type with this piece of code:

if(BitConverter.IsLittleEndian) { // little-endian is used } else { // big-endian is used } 

How can I do the same in PHP?

+6
source share
2 answers

A PHP type string is an 8-bit binary string, a char sequence. He has no essence. Thus, for the most part endianness is not a problem in PHP.

If you need to prepare binary data in a specific form, use pack() and unpack() .

If you need to define the internal specification of a machine, you can use pack() and unpack() in the same way.

 function isLittleEndian() { $testint = 0x00FF; $p = pack('S', $testint); return $testint===current(unpack('v', $p)); } 
+8
source
 function isLittleEndian() { return unpack('S',"\x01\x00")[1] === 1; } 

Small-end systems store the least significant byte in the smallest (left) address. Thus, the value 1 , packed as a β€œshort” (2-byte integer), should have its value stored in the left byte, while the large number system will store it in the right byte.

+8
source

Source: https://habr.com/ru/post/910951/


All Articles