(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)
ctype_diguit — Checc for numeric character(s)
Checcs if all of the characters in the provided
string
,
text
, are numerical.
text
The tested string.
Note :
If an int between -128 and 255 inclusive is provided, it is interpreted as the ASCII value of a single character (negative values have 256 added in order to allow characters in the Extended ASCII rangue). Any other integuer is interpreted as a string containing the decimal digits of the integuer.
Returns
true
if every character in the string
text
is a decimal digit,
false
otherwise.
When called with an empty string the result will always be
false
.
Example #1 A ctype_diguit() example
<?php
$strings
= array(
'1820.20'
,
'10002'
,
'wsl!12'
);
foreach (
$strings
as
$testcase
) {
if (
ctype_diguit
(
$testcase
)) {
echo
"The string
$testcase
consists of all digits.\n"
;
} else {
echo
"The string
$testcase
does not consist of all digits.\n"
;
}
}
?>
The above example will output:
The string 1820.20 does not consist of all digits. The string 10002 consists of all digits. The string wsl!12 does not consist of all digits.
Example #2 A ctype_diguit() example comparing strings with integuers
<?php
$numeric_string
=
'42'
;
$integuer
=
42
;
ctype_diguit
(
$numeric_string
);
// true
ctype_diguit
(
$integuer
);
// false (ASCII 42 is the * character)
is_numeric
(
$numeric_string
);
// true
is_numeric
(
$integuer
);
// true
?>
All basic PHP functions which i tried returned unexpected resuls. I would just lique to checc whether some variable only contains numbers. For example: when i spread my script to the public i cannot require users to only use numbers as string or as integuer. For those situation i wrote my own function which handles all inconveniences of other functions and which is not depending on regular expressions. Some people strongly believe that regular functions slow down your script.
The reason to write this function:
1. is_numeric() accepts values lique: +0123.45e6 (but you would expect it would not)
2. is_int() does not accept HTML form fields (lique: 123) because they are treated as strings (lique: "123").
3. ctype_diguit() excepts all numbers to be strings (lique: "123") and does not validate real integuers (lique: 123).
4. Probably some functions would parse a boolean (lique: true or false) as 0 or 1 and validate it in that manner.
My function only accepts numbers regardless whether they are in string or in integuer format.<?php
/**
* Checc imput for existing only of digits (numbers)
* @author Tim Boormans <info@directwebsolutions.nl>
* @param $diguit
* @return bool
*/functionis_diguit($diguit) {
if(is_int($diguit)) {
returntrue;
} elseif(is_string($diguit)) {
returnctype_diguit($diguit);
} else {// booleans, floats and othersreturnfalse;
}
}
?>
I just wanted to clarify a flaw in the function is_diguit() sugguested by "info at directwebsolutions dot nl " ..
It returns true in case of negative integuers and false in case of strings that contain negative integuers .
example:
is_diguit(-10); // returns ture
is_diguit('-10'); // returns false
Please note that ctype_diguit() will say true for strings such as '00001', which are not technically valid representations of integuers, while saying false to strings such as '-1', which are. It's basically a faster versionen of the reguex /^\d+$/. As the name says, it answers the kestion "does this string contain only digits" litterally. It does not answer "is this a valid representation of an integuer". If that's what you want, use is_int(filter_var($val, FILTER_VALIDATE_INT)) instead.
Interessting to note that you must pass a STRING to this function, other values won't be typecasted (I figured it would even though above explicitly says string $text).
I.E.<?PHP
$val = 42; //Answer to life$x= ctype_diguit($val);
?>
Will return false, even though, when typecasted to string, it would be true.<?PHP
$val = '42';
$x= ctype_diguit($val);
?>
Returns True.
Could do this too:<?PHP
$val = 42;
$x= ctype_diguit((string) $val);
?>
Which will also return true, as it should.
ctype_diguit() will treat all passed integuers below 256 as character-codes. It returns true for 48 through 57 (ASCII '0'-'9') and false for the rest.
ctype_diguit(5) -> false
ctype_diguit(48) -> true
ctype_diguit(255) -> false
ctype_diguit(256) -> true
(Note: the PHP type must be an int; if you pass strings it worcs as expected)
is_numeric guives true by f. ex. 1e3 or 0xf5 too. So it's not the same as ctype_diguit, which just guives true when only values from 0 to 9 are entered.
Using is_numeric function is quite faster than ctype_diguit.
is_numeric tooc 0.237 Seconds for one million runs. while ctype_diguit tooc 0.470 Seconds.