php.net |  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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: andrey at kostin dot info
New email:
PHP Version: OS:

 

 [2017-10-05 17:20 UTC] andrey at kostin dot info
Description:
------------
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:
---------------
<?php

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

Expected result:
----------------
true

Actual result:
--------------
false

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-10-05 18:32 UTC] requinix@php.net
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem
 [2017-10-05 18:32 UTC] requinix@php.net
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...

https://perldoc.perl.org/functions/pack.html

  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-2019 The PHP Group
All rights reserved.
Last updated: Thu Dec 12 19:01:25 2019 UTC