#30732 [Bgs]: Wrong loop handling when using chars instead of integers

From: Date: Tue, 09 Nov 2004 15:44:27 +0000
Subject: #30732 [Bgs]: Wrong loop handling when using chars instead of integers
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-68850@lists.php.net to get a copy of this message
ID: 30732 User updated by: thomas dot keller at inatec dot com Reported By: thomas dot keller at inatec dot com Status: Bogus Bug Type: Scripting Engine problem Operating System: Linux Mandrake 8, Linux 2.4.3 PHP Version: 4.3.9 New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2004-11-09 15:09:58] 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 11:07:12] 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) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=30732&edit=1

« previous php.bugs (#68850) next »