PHPackages                             danog/phpstruct - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. danog/phpstruct

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

danog/phpstruct
===============

PHP implementation of python's struct module.

1.2(9y ago)1110.1k↓33.3%4MITPHPPHP &gt;=5.6.0CI failing

Since Jul 4Pushed 4y ago3 watchersCompare

[ Source](https://github.com/danog/PHPStruct)[ Packagist](https://packagist.org/packages/danog/phpstruct)[ Docs](https://daniil.it/phpstruct)[ RSS](/packages/danog-phpstruct/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (16)Used By (0)

PHPStruct class
===============

[](#phpstruct-class)

[![Build Status](https://camo.githubusercontent.com/450e6d66745e22644b3f159708473b87ccd7a6fe48799de9072873eeecf33fb0/68747470733a2f2f7472617669732d63692e6f72672f64616e6f672f5048505374727563742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/danog/PHPStruct)[![Codacy Badge](https://camo.githubusercontent.com/53b91564158eda821bc3a2c4ba5e5c805f76462dc295b0827cc50d39bb4a32e4/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3762393165333065633839613433313362646233343736366561393930313133)](https://www.codacy.com/app/daniil-gentili-dg/PHPStruct?utm_source=github.com&utm_medium=referral&utm_content=danog/PHPStruct&utm_campaign=Badge_Grade)[![License](https://camo.githubusercontent.com/6d24b3f3b01b34472a3ec282b374ab75706b14e5bfffb487ce6c412839823151/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f64616e6f672f7068707374727563742e7376673f6d61784167653d323539323030303f7374796c653d666c61742d737175617265)](https://opensource.org/licenses/MIT)[![Packagist download count](https://camo.githubusercontent.com/9db6f937765ed82b0fc6350287c01f2081fa9b77fdf2ced475640d8528beddc5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f64616e6f672f7068707374727563742e7376673f6d61784167653d323539323030303f7374796c653d666c61742d737175617265)](https://packagist.org/packages/danog/phpstruct)[![Packagist](https://camo.githubusercontent.com/415c512bc7fbe194a96453d8aac19bf6ccb1b2ad9829d6ab489286c5425ef092/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64616e6f672f5048505374727563742e7376673f6d61784167653d323539323030303f7374796c653d666c61742d737175617265)](https://packagist.org/packages/danog/phpstruct)[![HHVM Status](https://camo.githubusercontent.com/da90ee862f459fc5779c90f28b500560d475360c289e9cab631fb30372fcfaa4/687474703a2f2f6868766d2e683463632e64652f62616467652f64616e6f672f7068707374727563742e7376673f7374796c653d666c61742d737175617265)](http://hhvm.h4cc.de/package/danog/phpstruct)[![StyleCI](https://camo.githubusercontent.com/df476214cb0385f22788369bcfa6baf615a46c7cbab1151dbc88ad15538f479f/68747470733a2f2f7374796c6563692e696f2f7265706f732f36323435343133342f736869656c64)](https://styleci.io/repos/62454134)

Licensed under MIT.

PHP implementation of Python's struct module.

This library was created to help me develop a [client for the mtproto protocol](https://github.com/danog/MadelineProto).
It supports php 5.6, php 7 and HHVM.

The functions and the formats are exactly the ones used in python's struct ()

This library can be used to pack/unpack strings, ints, floats, chars and bools into bytes. It has lots of advantages over PHP's native implementation of pack and unpack, such as:

- Custom byte endianness.
- Lots of useful formats that aren't present in the native implementation.
- The syntax of the format string of pack and unpack is the same as in python's struct module.
- The result of unpack is normal numerically indexed array that starts from 0 like it should.
- The result of unpack has type casted values (int for integer formats, bool for boolean formats, float for float formats and string for all of the other formats).
- The calcsize function is implemented.
- The q and Q formats can be used even on 32 bit systems (the downside is limited precision).
- Padding is supported for the @ modifier.

For now custom byte size may not work properly on certain machines for the f and d formats.

Installation
------------

[](#installation)

Install using composer:

```
composer require danog/phpstruct

```

Usage
=====

[](#usage)

Dynamic (recommended)

```
require('vendor/autoload.php');
$struct = new \danog\PHP\StructClass();
$pack = $struct->pack("2cxi", "ab", 44);
$unpack = $struct->unpack("2cxi", $pack);
var_dump($unpack);
$count = $struct->calcsize("2cxi");

```

Dynamic (while specifying format string during istantiation)

```
require('vendor/autoload.php');
$struct = new \danog\PHP\StructClass("2cxi");
$pack = $struct->pack("ab", 44);
$unpack = $struct->unpack($pack);
var_dump($unpack);
$count = $struct->size;
$formatstring = $struct->format;

```

Static

```
require('vendor/autoload.php');
$pack = \danog\PHP\Struct::pack("2cxi", "ab", 44);
$unpack = \danog\PHP\Struct::unpack("2cxi", $pack);
var_dump($unpack);
$count = \danog\PHP\Struct::calcsize("2cxi");

```

[Daniil Gentili](http://daniil.it)

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 98.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~10 days

Recently: every ~23 days

Total

14

Last Release

3472d ago

Major Versions

0.4.2 → 1.02016-07-28

### Community

Maintainers

![](https://www.gravatar.com/avatar/e1586cd637028e4f8da85f2252b8f4b81e05f6b17a93f92c34ff064dd838e6bb?d=identicon)[danog](/maintainers/danog)

---

Top Contributors

[![danog](https://avatars.githubusercontent.com/u/7339644?v=4)](https://github.com/danog "danog (86 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (1 commits)")

---

Tags

stringdecimalintegerbinarypythonfloatbyteunpackpackbytesstruct

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/danog-phpstruct/health.svg)

```
[![Health](https://phpackages.com/badges/danog-phpstruct/health.svg)](https://phpackages.com/packages/danog-phpstruct)
```

###  Alternatives

[brick/math

Arbitrary-precision arithmetic library

2.1k504.0M277](/packages/brick-math)[nelexa/buffer

Reading And Writing Binary Data (incl. primitive types, ex. byte, ubyte, short, ushort, int, uint, long, float, double). The classes also help with porting the I/O operations of the JAVA code.

33521.7k4](/packages/nelexa-buffer)[phpinnacle/buffer

PHPinnacle binary buffer implementation

2493.7k10](/packages/phpinnacle-buffer)[danielstjules/sliceable-stringy

Python string slices in PHP

4751.6k1](/packages/danielstjules-sliceable-stringy)[aza/math

AzaMath - Anizoptera CMF mathematic component. Arbitrary precision arithmetic (for huge integers; BCMath wrapper) and universal convertor between positional numeral systems (supported bases from 2 to 62 inclusive, and systems with custom alphabet; pure PHP realisation, can use GMP and core PHP functions for speed optimization).

1921.9k1](/packages/aza-math)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
