|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #75320 Pack uses "a" and "Z" differently
Submitted: 2017-10-05 17:20 UTC Modified: 2017-10-05 18:32 UTC
From: andrey at kostin dot info Assigned:
Status: Verified Package: Strings related
PHP Version: 7.1.10 OS: Ubuntu 16.04, Gentoo
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: andrey at kostin dot info
New email:
PHP Version: OS:


 [2017-10-05 17:20 UTC] andrey at kostin dot info
I can be wrong, but documentation says that "a" and "Z" are equal since PHP 5.5
But it is not:
php > var_dump(pack('a1', '1'));
string(1) "1"
php > var_dump(pack('Z1', '1'));
string(1) ""

If I'm wrong, please point to correct docs.

Test script:

var_dump(pack('a1', '1') === pack('Z1', '1'));

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-10-05 18:32 UTC]
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem
 [2017-10-05 18:32 UTC]
In this case the correct docs are Perl's :D
We're borrowing pack/unpack from them anyways, and their explanations go more in depth. Maybe we should link to them...

  a  A string with arbitrary binary data, will be null padded.
  Z  A null-terminated (ASCIZ) string, will be null padded.

described as

  The a , A , and Z types gobble just one value, but pack it as a string of length
  count, padding with nulls or spaces as needed. When unpacking, A strips trailing
  whitespace and nulls, Z strips everything after the first null, and a returns
  data with no stripping at all.

  If the value to pack is too long, the result is truncated. If it's too long and
  an explicit count is provided, Z packs only $count-1 bytes, followed by a null
  byte. Thus Z always packs a trailing null, except when the count is 0.

I think we can fit the terminating NUL and truncation behavior briefly in the table. Something like "NUL-padded string; always includes at least one NUL terminator". (The "new in PHP 5.5" is covered in the Changelog already.) Or just mention the terminator and add a Note?

One more small change to make: the `format` description says
> For a, A, h, H the repeat count specifies how many characters of one data
> argument are taken,
That applies to Z too.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Jun 19 04:01:31 2024 UTC