php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26914 Comparison Operators not working with alpha characters
Submitted: 2004-01-15 04:09 UTC Modified: 2004-01-15 04:52 UTC
From: rschwartz at ec dot rr dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.4 OS: Any
Private report: No CVE-ID: None
 [2004-01-15 04:09 UTC] rschwartz at ec dot rr dot com
Description:
------------
I was testing a for loop with incrementing alpha characters.  For example, looping through a-z and printing those characters.  It does not seem to work as expected.

Reproduce code:
---------------
Ex 1:

for ($i = 'a'; $i <= 'z'; $i++) {
	echo $i;
}

Ex 2:

for ($i = 'a'; $i < 'z'; $i++) {
	echo $i;
}

Ex 3:

for ($i = 'a'; $i < 'aa'; $i++) {
	echo $i;
}

Expected result:
----------------
Ex 1:   Print a-z.

Ex 2:   Print a-y.

Ex 3:   Print a-z.

Actual result:
--------------
Ex1 loops through to 'yz' and does not work as expected.
Ex2 loops through to 'z', which is expected.
Ex3 dies after the first iteration and only prints 'a'.

I think this is an odd behavior.  The < operator works as expected in this example but the <= operator does not.  It doesn't seem like a consistent behavior.  If I can remember correctly, I was able to get this to work on an earlier version sometime ago.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-15 04:18 UTC] psyko at wyred dot mine dot nu
Same for version 4.3.3
 [2004-01-15 04:52 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

Using ++ on a string is defined to increment 'z' -> 'aa' -> ... -> 'az' -> 'ba' etc.  The first value in this sequence which fails the <='z' test is 'za' (the value after 'yz'), which explains the observed behaviour of your 1st example.

In the 3rd example, your expectation is just plain wrong. After the first iteration, 'a' will be incremented to 'b', which is plainly not <= 'aa', so the loop terminates immediately.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 14 12:01:33 2025 UTC