> -----Original Message-----
> From: Peter Hutnick [mailto:[EMAIL PROTECTED]]
> Sent: 14 January 2003 15:49
> 
> I can't get my head around dealing with all the magic that 
> PHP does to my
> data.

You need to read, very carefully, several times, the PHP manual section on
Type Juggling: http://www.php.net/manual/en/language.types.type-juggling.php

>  I'm trying to do the following (in C++):
> 
>    char a = 'a';
>    a = a + 13;
> 
> The resulting value of a is 'n'.  If I do the equivalent in 
> PHP ($a = "a";
> $a += 13;) the resulting value is 13.  I tried (int)$a += 
> 13;, but that
> doesn't make a difference.

Well, in an expression involving the arithmetic operators +-*/, and their
assignment equivalents += etc, all values are automatically converted to
numbers first -- and the numeric conversion of any string not starting with
a digit is 0.  So in your statement

   $a += 13

the content of $a (="a") is first converted to an integer, giving zero, and
then has 13 added to it -- hence 13.

> Is there some "whole different way of thinking" I should be 
> engaged in?

Yes -- probably several -- but in this case I'd say the concepts of loose
typing and automatic type conversion which depends on context, as well as
the way in which the various types get converted from one to another.
Because automatic type-conversion is performed so frequently by PHP, there
is actually very little use for the cast operators, although they do exist.

> Is there some way to make PHP add an int to the ASCII value 
> of a single
> character string (since there seems to be no char type)?

You need ord() and chr() -- yes, PHP is not the most elegant language in
this respect.

(By the by, watch out for the behaviour of ++ and -- when applied to
strings, as it probably isn't what you'd expect from the foregoing: {$b =
"a"; $b++;} will result in $b containing "b"!  And even then it's not
straightforward or totally intuitive -- before you use this behaviour,
figure out why {for ($a="a"; $a<="z"; $a++) echo $a;} *doesn't* display the
letters from a to z!!)

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to