Getting / checking number of digits of a positive number
Moderator: General Moderators
Getting / checking number of digits of a positive number
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.
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
strlen is going to give incorrect results if the number is not an integer
Re: Getting / checking number of digits of a positive number
What kind of results do you expect for these numbers?
$a = -17.3;
$b = -17.300;
$c = 0123;
$d = 1.52e6;
$e = 0xff;
$a = -17.3;
$b = -17.300;
$c = 0123;
$d = 1.52e6;
$e = 0xff;
Re: Getting / checking number of digits of a positive number
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):
Am I doing something wrong here?!
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;
?>
- 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
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.
- 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
Needs to be strlen(strval($id)) instead.
- 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
It will still be 9 thus $number won't be set.Jonah Bron wrote:Needs to be strlen(strval($id)) instead.
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.
Re: Getting / checking number of digits of a positive number
first if should take care of that 9 AbraCadaver
you are right in the == usage instead of =
Code: Select all
if (strlen($id) >= 4) {
$number = substr($id, -4);
}- 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
DOH!!!mikosiko wrote:first if should take care of that 9 AbraCadaver
you are right in the == usage instead of =Code: Select all
if (strlen($id) >= 4) { $number = substr($id, -4); }
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.
Re: Getting / checking number of digits of a positive number
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.
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.