Page 1 of 1

Return number of characters in a variable

Posted: Fri Dec 23, 2005 2:32 pm
by dwessell
Hi all..

If I have a variable like:

$var = 1234567890;

How can I return the number of char's in that variable?



As well, while we're at it.. Am I correct in that you don't have to specify what type of variable you are defining, but php does it for you automatically?

What if I wanted to, could I?

Could I do something like:

int $var = 1234567890;


Thanks
David

Posted: Fri Dec 23, 2005 2:46 pm
by Ambush Commander
Well, it's a number, but I suppose you could try strlen().

Any no, you can't statically type a variable. PHP is dynamically typed. In PHP5, however, you can give type hints in function parameters.

Posted: Fri Dec 23, 2005 2:56 pm
by josh

Code: Select all

$var = (int)6544645;
$var = (float)6544645;

Posted: Fri Dec 23, 2005 3:15 pm
by MaNiAC
or later on in the script

Code: Select all

settype($var, "string");

Posted: Fri Dec 23, 2005 9:34 pm
by Jenk
or:

Code: Select all

$int = intval(123);
$str = strval('123');
Though jshpro2's suggestion is the most efficient and is the "proper" way.

But in PHP, using quotes signifies that it is a string, no quotes and numerics signifies it is an int, numerics with a decimal signifies it is a float.