Page 1 of 1
Getting / checking number of digits of a positive number
Posted: Thu May 27, 2010 8:25 am
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.
Re: Getting / checking number of digits of a positive number
Posted: Thu May 27, 2010 8:43 am
by internet-solution
Re: Getting / checking number of digits of a positive number
Posted: Thu May 27, 2010 8:54 am
by mikosiko
strlen is going to give incorrect results if the number is not an integer
Re: Getting / checking number of digits of a positive number
Posted: Thu May 27, 2010 9:37 am
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;
Re: Getting / checking number of digits of a positive number
Posted: Thu May 27, 2010 10:13 am
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?!
Re: Getting / checking number of digits of a positive number
Posted: Thu May 27, 2010 10:37 am
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???
Re: Getting / checking number of digits of a positive number
Posted: Thu May 27, 2010 10:52 am
by Jonah Bron
Needs to be strlen(strval($id)) instead.
Re: Getting / checking number of digits of a positive number
Posted: Thu May 27, 2010 12:09 pm
by AbraCadaver
Jonah Bron wrote:Needs to be strlen(strval($id)) instead.
It will still be 9 thus $number won't be set.
Re: Getting / checking number of digits of a positive number
Posted: Thu May 27, 2010 12:19 pm
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 =
Re: Getting / checking number of digits of a positive number
Posted: Thu May 27, 2010 12:25 pm
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!!!

Re: Getting / checking number of digits of a positive number
Posted: Thu May 27, 2010 1:57 pm
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.