Posted by Kimmo Laine on 04/12/06 10:51
"Tijs Verwest" <stephane.vollet@bluewin.ch> wrote in message
news:443c0c23$1_2@news.bluewin.ch...
> It's a newbie question...
>
> Is it possible to type variable in PHP inspite this is a loose type
> language.
Variables do have some sort of typing but as you know it's very loose. if a
particular variable happens to be an integer and you assign an array to it,
it becomes an array. There are ways of detecting the type of a variable and
there are ways of casting a variable to certain type.
A little exaple of ways of manipulating the type.
<?php
$foo; // Now we have no idea what this is.
$foo = "bar"; // assigning a string to it makes it a string.
echo gettype($foo); // this prints the type. string
$bar = (int)$foo; // now we assign the string into $bar but typecast it to
int.
if(is_int($bar)) echo "$bar is an integer"; // is it int?
else echo "wtf? $bar is not an int, it's ". get_type($bar);
?>
I recommend you to read this: http://fi2.php.net/manual/en/ref.var.php
> Or would that be required in some cases?
It depends on your code. Sometimes it's crucial, sometimes not. If and when
it is, I use type casts to make sure something is of some type, for example
when inserting a number intoa database I make sure it's a number to avoid
errors. A simple type cast takes care of it.
--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)
[Back to original message]
|