Return number of characters in a variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dwessell
Forum Commoner
Posts: 62
Joined: Fri Dec 23, 2005 2:30 pm

Return number of characters in a variable

Post 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
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

$var = (int)6544645;
$var = (float)6544645;
MaNiAC
Forum Newbie
Posts: 20
Joined: Fri Dec 23, 2005 4:20 am

Post by MaNiAC »

or later on in the script

Code: Select all

settype($var, "string");
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
Post Reply