You are here: Re: I hate this language :-D « All PHP « IT news, forums, messages
Re: I hate this language :-D

Posted by Janwillem Borleffs on 08/19/06 11:11

Dennis de Wit wrote:
> Just figured out... $a++ has a different meaning from $a+=1 when used
> on a string. Don't ask me why anyone would use number calculations on
> strings, but why the hell does ++ behave different on this matter?
>
> Why can't i say 'aa' + 'bb' and make it turn into 'cc'? That would be
> the same logic!
>

When you do $a += 1, PHP knows that you want to add the integer 1 to $a.
Therefore $a + 1 equals 0 + 1 = 1 (as the string 'a' is converted to a 0,
because it doesn't represent a valid integer value).

With $a++, PHP will behave like it gets $a's numeric representation as its
ASCII value and adds 1. The following will print the alphabet for you:

$a = 'A';
print $a;

while ('Z' != $a) {
$a++;
print $a;
}

Note that this only works for a-z & A-Z; if you try it on, say, '@', the
value will remain unchanged.

FYI, Perl behaves the same with the a-z & A-Z ranges, but returns 1 for the
$a++ operation with other characters.


JW

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация