Getting / checking number of digits of a positive number

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
webman
Forum Newbie
Posts: 13
Joined: Thu May 27, 2010 8:20 am

Getting / checking number of digits of a positive number

Post by webman »

Hello! I am new here! I want to ask is there a php function that tests how many digits a number consists of ?! i.e. for 156 returns 3, for 4 returns 1 and for 55981 returns 5 ?!

Thanks in advance.
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: Getting / checking number of digits of a positive number

Post by internet-solution »

mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Getting / checking number of digits of a positive number

Post by mikosiko »

strlen is going to give incorrect results if the number is not an integer
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Getting / checking number of digits of a positive number

Post by Apollo »

What kind of results do you expect for these numbers?

$a = -17.3;
$b = -17.300;
$c = 0123;
$d = 1.52e6;
$e = 0xff;
webman
Forum Newbie
Posts: 13
Joined: Thu May 27, 2010 8:20 am

Re: Getting / checking number of digits of a positive number

Post by webman »

Thanks for the replies! I have found the function I have been looking for. But still I am new to php and need some further help. Would you please tell me why this code returns emtpy screen (not working I guess):

Code: Select all

<?php
$id = 687675883;
if (strlen($id) >= 4) {
$number = substr($id, -4);
}
elseif (strlen($id) = 3) {
$number = substr($id, -3);
}
elseif (strlen($id) = 2) {
$number = substr($id, -2);
}
elseif (strlen($id) = 1) {
$number = substr($id, -1);
}
echo $number;
?>
Am I doing something wrong here?!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Getting / checking number of digits of a positive number

Post by AbraCadaver »

You need to use == instead of = in your comparison expressions. Also. the strlen of $id is 9 and you don't have a condition for that. What are you trying to do???
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Getting / checking number of digits of a positive number

Post by Jonah Bron »

Needs to be strlen(strval($id)) instead.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Getting / checking number of digits of a positive number

Post by AbraCadaver »

Jonah Bron wrote:Needs to be strlen(strval($id)) instead.
It will still be 9 thus $number won't be set.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Getting / checking number of digits of a positive number

Post by mikosiko »

first if should take care of that 9 AbraCadaver

Code: Select all

if (strlen($id) >= 4) {
$number = substr($id, -4);
}
you are right in the == usage instead of =
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Getting / checking number of digits of a positive number

Post by AbraCadaver »

mikosiko wrote:first if should take care of that 9 AbraCadaver

Code: Select all

if (strlen($id) >= 4) {
$number = substr($id, -4);
}
you are right in the == usage instead of =
DOH!!! :oops:
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
webman
Forum Newbie
Posts: 13
Joined: Thu May 27, 2010 8:20 am

Re: Getting / checking number of digits of a positive number

Post by webman »

Thank you all for the help you have provided me and possibly others here. I have managed to make it work now. I needed your help and php error messages that I have now turned on.

I needed the code in order to get the 3rd and 4th digits from back of article numbers. I need those numbers in order to create custom file directories.
Post Reply