php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30732 Wrong loop handling when using chars instead of integers
Submitted: 2004-11-09 11:07 UTC Modified: 2004-11-09 16:44 UTC
From: thomas dot keller at inatec dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.9 OS: Linux Mandrake 8, Linux 2.4.3
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: thomas dot keller at inatec dot com
New email:
PHP Version: OS:

 

 [2004-11-09 11:07 UTC] thomas dot keller at inatec dot com
Description:
------------
Configure command:
'./configure' '--prefix=/usr/local/php' '--with-apxs2=/home/apache2/apache-2.0.52/bin/apxs' '--enable-versioning' '--with-mysql=/usr/local/mysql' '--with-gd' '--with-zlib'

I use the following code snippet to print out an array
of chars in alphabetic order. If I have the condition
$i < 'Z' (so only until 'Y'), everything works fine, 
for the condition(s) $i <= 'Z' or $i < ('A' + 26)
PHP produces wrong results.

I could reproduce the error on a Redhat 9 system with PHP 4.3.4.

Reproduce code:
---------------
$letters = array();

for ($i='A'; $i<='Z'; $i++) print $i;



Expected result:
----------------
ABCDEFGHIJKLMNOPQRSTUVWXYZ

(26 ^ 1 letters)

Actual result:
--------------
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABAC .. YZ

(26 ^ 2 letters)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-11-09 15:09 UTC] mgf@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

When you use ++ on strings, the sequence of values is:

... \'X\', \'Y\', \'Z\', \'AA\', \'AB\', ... \'AZ\', \'BA\', ...

Only when this sequence reaches \'ZA\' does it fail the <=\'Z\' test.
 [2004-11-09 16:44 UTC] thomas dot keller at inatec dot com
Hrm... I thought of 'Z' not as a string, but
as a single character. In other programming
languages (C/C++, Java) a char is a signed
BYTE, thus can be used as replacement for
integer values. So I expect I need to write

for ($i=ord('A');$i<=ord('Z'); $i++) print chr(i);

to get my desired output? Since I don't know
a PHP shorthand for Perl's

foreach ('A' .. 'Z') print $_;

Thanks for clearing that up.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Aug 13 10:00:03 2025 UTC