= 4.0.4, PHP 5, PHP 7, PHP 8)ctype_digit — 做純數(shù)字檢測說明ctype_digit(string $text): bool檢查提供的 string 和 text 里面的字符是不是都是數(shù)字。 參數(shù)text需要被測試的字符串">

ctype_digit

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

ctype_digit做純數(shù)字檢測

說明

ctype_digit(string $text): bool

檢查提供的 stringtext 里面的字符是不是都是數(shù)字。

參數(shù)

text

需要被測試的字符串。

返回值

如果 text 字符串是一個十進制數(shù)字,就返回 true ;反之就返回 false 。

更新日志

版本 說明
5.1.0 在 PHP 5.1.0 之前,當 text 是一個空字符串的時候,該函數(shù)將返回 true 。

范例

示例 #1 一個 ctype_digit() 例子

<?php
$strings 
= array('1820.20''10002''wsl!12');
foreach (
$strings as $testcase) {
    if (
ctype_digit($testcase)) {
        echo 
"The string $testcase consists of all digits.\n";
    } else {
        echo 
"The string $testcase does not consist of all digits.\n";
    }
}
?>

以上例程會輸出:

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.

示例 #2 一個通過 ctype_digit() 來比對字符和整數(shù)的例子。

<?php

$numeric_string 
'42';
$integer        42;

ctype_digit($numeric_string);  // true
ctype_digit($integer);         // false (ASCII 42 is the * character)

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

注釋

注意:

這個函數(shù)的參數(shù)要求是一個 string 這一點是非常有用的,因此當你傳入一個 integer 的參數(shù)也許不能得到期望的結果。然后,同樣需要注意HTML表單將會返回數(shù)字字符串而不是一個整型。 看下手冊的 types 章節(jié)。

注意:

如果給出一個 -128 到 255 之間(含)的int, 將會被解釋為該值對應的ASCII字符 (負值將加上 256 以支持擴展ASCII字符). 其它整數(shù)將會被解釋為該值對應的十進制字符串.

參見