php / php/doc-en

Function: sprintf, misleading documentation

Open
#2,447 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Category: Strings Extension: standard
Dominant language
XML
Stars
596
Forks
890
Avg merge
1d 15h
Merged PRs (30d)
55

Description

Page: https://www.php.net/manual/en/function.sprintf.php

The confusion is created by the fact that nowhere is stated that sprintf format can accept mulitple flags, even if they are mutually exclusive, like '(char) and 0 or space ( ).
The confusion is brought to light, and actually forced on us, by the specific example:

Example #2 Specifying padding character
<?php
echo sprintf("%'.9d\n", 123);
echo sprintf("%'.09d\n", 123);
?>

I guess you know * , this code sprintf("%'.09d\n", 123); uses two flags, flag '. and flag 0.
Since they can not both be printed, only the last one is used.
But when we read this, and it's not explained, it's very easy like me to lose 40' of your day only to understand what is going on.


So, please fix it, like for example here:

Example #2 Specifying padding character
<?php
echo sprintf("%'.9d\n", 123);
echo sprintf("%'.09d\n", 123); → /* Use of multiple exclusive flags '. and 0 result only the last one to be used */ ←
?>

and after the Flag | Description,
Warning

The flags '(char), (space), 0, are mutually exclusive. If many of them are used on the same format, the last one is applied.


Finally, than you all, for this great tool, PHP! It has allowed many to do great things. Sometimes though I pick my hair out of despair that we are year 2023, PHP8, and PHP is still struggling with things like this XD

Thank you,
have a good day :)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open the sprintf manual page and inspect Example #2, the flag descriptions, and the surrounding format-string guidance. Update the documentation to explain multiple mutually exclusive padding flags and clarify which flag is applied when they appear together, then verify the example and warning read consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.