(PHP 4 >= 4.0.4, PHP 5, PHP 7)

ctype_digitAuf Ziffern überprüfen


bool ctype_digit ( string $text )

Prüft ob der übergebene String nur aus Ziffern besteht.



Der zu prüfende String.


Liefert TRUE wenn jedes Zeichen in text eine Ziffer ist, ansonsten FALSE.


Version Beschreibung
5.1.0 Vor PHP 5.1.0 gab die Funktion auch TRUE zurück wenn der Eingabestring komplett leer war.


Beispiel #1 ctype_digit() Beispiel

= array('1820.20''10002''wsl!12');
foreach (
$strings as $testcase) {
    if (
ctype_digit($testcase)) {
"Der String $testcase besteht aus Ziffern.\n";
    } else {
"Der String $testcase enthält nicht nur Ziffern.\n";

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

Der String 1820.20 enthält nicht nur Ziffern.
Der String 10002 besteht aus Ziffern.
Der String wsl!12 enthält nicht nur Ziffern.

Beispiel #2 A ctype_digit() example comparing strings with integers


$integer        42;

ctype_digit($numeric_string);  // true
ctype_digit($integer);         // false

is_numeric($numeric_string);   // true
is_numeric($integer);          // true



Diese Funktion ist nur sinnvoll wenn der Eingabeparameter ein string ist, ein integer Wert als Eingabe wird immer FALSE als Ergebnis liefern. Da auch numerische HTML-Formulareingaben immer als String übergeben werden hat die Funktion hier dennoch ihren Sinn. Siehe auch den types Abschnitt des Handbuchs.


Wenn ein integer zwischen -128 und 255 (inklusive) übergeben wird, wird dieser als ASCII Wert eines einzelnen Buchstabens interpretiert (zu negativen Werten wird 256 dazu addiert, um Buchstaben des Erweiterten ASCII Zeichensatzes zu erlauben). Alle anderen Integer werden wie ein String interpretiert, welcher die dezimalen Ziffern des Integers enthält.

Siehe auch

  • ctype_alnum() - Auf alphanumerische Zeichen überprüfen
  • ctype_xdigit() - Auf Hexadezimalziffern überprüfen
  • is_numeric() - Prüft, ob eine Variable eine Zahl oder ein numerischer String ist
  • is_int() - Prüft, ob eine Variable vom Typ int ist
  • is_string() - Prüft, ob Variable vom Typ string ist

add a note add a note

User Contributed Notes 12 notes

info at directwebsolutions dot nl
5 years ago
All basic PHP functions which i tried returned unexpected results. I would just like to check 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 integer. 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 like: +0123.45e6 (but you would expect it would not)
2. is_int() does not accept HTML form fields (like: 123) because they are treated as strings (like: "123").
3. ctype_digit() excepts all numbers to be strings (like: "123") and does not validate real integers (like: 123).
4. Probably some functions would parse a boolean (like: 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 integer format.
     * Check input for existing only of digits (numbers)
     * @author Tim Boormans <info@directwebsolutions.nl>
     * @param $digit
     * @return bool
function is_digit($digit) {
is_int($digit)) {
        } elseif(
is_string($digit)) {
        } else {
// booleans, floats and others
return false;
strrev xc tod noxeh ta ellij
8 years ago
ctype_digit() will treat all passed integers below 256 as character-codes. It returns true for 48 through 57 (ASCII '0'-'9') and false for the rest.

ctype_digit(5) -> false
ctype_digit(48) -> true
ctype_digit(255) -> false
ctype_digit(256) -> true

(Note: the PHP type must be an int; if you pass strings it works as expected)
error17191 at gmail dot com
2 years ago
I just wanted to clarify a flaw in the function is_digit() suggested by "info at directwebsolutions dot nl " ..
It returns true in case of negative integers and false in case of strings that contain negative integers .
is_digit(-10); // returns ture
is_digit('-10'); // returns false
rlerne at gmail dot com
4 years ago
Interesting 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).


= 42; //Answer to life
$x = ctype_digit($val);

Will return false, even though, when typecasted to string, it would be true.

= '42';
$x = ctype_digit($val);

Returns True.

Could do this too:

= 42;
$x = ctype_digit((string) $val);

Which will also return true, as it should.
a_p_leeming at hotmail dot com
8 years ago
Also note that

<?php ctype_digit("-1");   //false ?>
Peter de Pijd
8 years ago
Note that an empty string is also false:
ctype_digit("") // false
smicheal2 at gmail dot com
2 years ago
Please note that ctype_digit() will say true for strings such as '00001', which are not technically valid representations of integers, while saying false to strings such as '-1', which are. It's basically a faster version of the regex /^\d+$/. As the name says, it answers the question "does this string contain only digits" literally. It does not answer "is this a valid representation of an integer". If that's what you want, use is_int(filter_var($val, FILTER_VALIDATE_INT)) instead.
John Saman
8 years ago
Using is_numeric function is quite faster than ctype_digit.

is_numeric took 0.237 Seconds for one million runs. while ctype_digit took 0.470 Seconds.
mdsky at web dot de
7 years ago
is_numeric gives true by f. ex. 1e3 or 0xf5 too. So it's not the same as ctype_digit, which just gives true when only values from 0 to 9 are entered.
6 years ago
If you need to check for integers instead of just digits you can supply your own function such as this:

function ctype_int($text)
preg_match('/^-?[0-9]+$/', (string)$text) ? true : false;
raul dot 3k at gmail dot com
9 years ago
The ctype_digit can be used in a simple form to validate a field:
= $_POST["field"];
"It's not a digit";

Digits is 0-9
zorrosNOSPAMwordsman at NOSPAM dot gmail dot com
1 year ago
If you want to verify whether or not a variable contains only digits, you can type cast it to a string and back to int and see if the result is identical. Like so:


// (bool) TRUE if only digits, FALSE otherwise
$isOnlyDigits = (string) (int) $input === (string) $input;


I haven't benchmarked it, but I'm guessing it's significantly faster then regular expressions.
To Top