php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #6107 increments are magic, decrements not
Submitted: 2000-08-11 08:03 UTC Modified: 2007-01-07 11:16 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:0 of 1 (0.0%)
From: waldschrott@php.net Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.3.2 OS:
Private report: No CVE-ID: None
 [2000-08-11 08:03 UTC] waldschrott@php.net
try this:

$a='a';
$a++;
$a--;
echo $a;

what do you expect? of course 'a', what do you get? 'b'

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-08-11 08:09 UTC] waldschrott@php.net
depending on POV it could be a feature request too...
 [2000-08-11 09:08 UTC] waldschrott@php.net
perhaps it?s more than I thought, try this:

echo $n='??';
echo ++$n;
echo ++$n;

what happens? nothing. If someone?s going to review that magical mechanism, he/she should document it OR improve it
 [2003-04-21 17:19 UTC] pollita@php.net
After some quick research it looks like what you describe is expected behavior and therefore should be documented, but not changed (at least not without some discussion on php.internals or zend.engine2).

In Zend/zend_operators.c:
-------------------------
ZEND_API int increment_function(zval *op1)
{
  .
  .
  .
  case IS_STRING:
    /* Perl style string increment */
    increment_string(op1);
  .
  .
  .
}

ZEND_API int decrement_function(zval *op1)
{
  .
  .
  .
  case IS_STRING:
    /* Like perl we only support string increment */
    /* Try to work with string as a number or leave it alone */
  .
  .
  .
}


The increment_string() function works from right to left, incrementing any character in the range [A-Za-z0-9], if the character is Z, z, or 9, it will roll over to A, a, or 0 respectively and 'carry-over' to the next alphanumeric.  Incrementing continues until no carry-over is encountered.

Personally, it seems to me that there should be a decrement_string() equivalent, but this is a messy thing to do with strings anyway.
 [2003-04-21 17:34 UTC] alindeman@php.net
I added a note and example in the docs.  Status -> Closed (wow, this is an old bug :)
 [2003-04-21 20:52 UTC] philip@php.net
Making this as a feature request again.
 [2007-01-07 11:16 UTC] bjori@php.net
This is not going to change, ever, and is documented 
behavior.
 [2020-02-07 06:12 UTC] phpdocbot@php.net
Automatic comment on behalf of alindeman
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=864e1edaf0a8b5e43a43a285171e16a071a7d679
Log: fix bug #6107
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Oct 04 05:01:27 2024 UTC