php / php/doc-en

unnecessary variable in the example of for loop

Open
#2,479 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
XML
Stars
596
Forks
890
Avg merge
1d 15h
Merged PRs (30d)
55

Description

From manual page: https://php.net/control-structures.for

 /* example 4 */
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);

This example 4 is clearly different and problematic compared to the other three examples. It demonstrates that in PHP, there can be multiple notations that yield the same output in a for loop. However, unlike the other examples, only example 4 includes the unnecessary variable $j. While the result remains the same, it is evident that adding $j is an unnecessary modification to this particular example.

According to https://bugs.php.net/bug.php?id=36877, https://github.com/php/doc-en/commit/29cf2d82f2b9a45e28e4ac0fdc672c9ec36b4c33, I investigated the origin of $j and discovered that it resulted from a documentation fix implemented in 2006. The report states:

The C-like clause for expr1 in
for ($i=0, $n=10; ...)
is accepted but not documented.
The corresponding C-like clause
$i=0, $j=1; outside a for is flagged as an error.

In my opinion, this modification was inappropriate. I believe it's more appropriate for the manual to provide explicit and clear explanations rather than relying on unnecessary variables for implicit demonstrations.

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

Start with the PHP manual page for control structures and inspect example 4 alongside the other for-loop examples. Review the linked bug report and historical commit for context; done means removing the unnecessary $j variable while preserving the example's intended output and explanation.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.