View and vote on the article here: A PHP coders diary Part1
A PHP coders diary Part1| Category | | | Summary | | | Body | "A PHP coders diary"
by noimnot (nonihil@gmail.com)
Part1 - Variable-Type-Classes
* Whats so Special about variables in PHP?
Variables in PHP are nice tools if you want to handle information in a type-unspecific manner, but that doesn't mean PHP is a "typeless" language, in fact it is, but its hiding it from you until you really need it..
* What?
Like in C, variables in PHP are assigned types, to get a headstart, here is a listing of the 8 types, supported by PHP:
Four scalar types:
*boolean
*integer
*float (floating-point number, aka 'double')
*string
Two compound types:
*array
*object
And finally two special types:
*resource
*NULL
(listing taken from the php.net manual ^^)
The manual also refers to occurences of type "double" which is simply a synonym for float, "the two names exist only for historic reasons".
* So why would i need type specific variables in PHP anyways?
Imagine the situation of having a string:
$string="123abc";
Taking the first character of this string, which in fact is a integer variable, but previously defined (the "" (quotes) around the string) as a an array of chars (string):
$num=$string{0};
echo $num;
would output "1", but if you wanted to do a mathematical operation on that "char", you would start to get into trouble, and thats where u need type specific variables, even in PHP, by doing:
$num=((int)string{0});
the "(int)" part of the above line is called a cast, which is simply the type you want your variable to be assigned to in brackets.
You can tell the PHP-interpreter that you want $num to be a variable of type integer, so you could perform any mathematical operation, which would not be necessary vica-verse.
* Ok, but how do i know when its time to use "casts"
To solve this problem, the PHP manual gives you "Appendix P. PHP type comparison tables". Which is a listing of possible type declarations and ways of controling/checking them at runtime.. Its pretty much self-explanatory which is why i will not give it any more voice... (until someone asks :)
last but not least, some
* Referrals:
http://php.net/ctype
http://php.net/manual/en/language.types.php
Written by: by noimnot (11 October 2005)
NoImNot is Member of Knowledge Bank Tutorial Writers (#619)
-----
{BACKLINKS(info=>hits|user,exclude=>SandBox,include_self=>0,noheader=>0)}{BACKLINKS} |
|
This article was imported from the CyberArmy University site. (original author: PixieLuv)
There are no replies to this post yet.
|