변수처리 is_numeric
(PHP 4, PHP 5, PHP 7, PHP 8)
is_numeric — 변수가 숫자인지 숫자 문자열인지 찾습니다.
설명
is_numeric(mixed $value
): bool
주어진 변수가 숫자인지 숫자 문자열인지 확인합니다.
매개변수
value
- 평가 중인 변수입니다.
반환 값
value
이 숫자 또는 숫자 문자열이면 true
를 반환하고 그렇지 않으면 false
를 반환합니다.
변경 로그
버전 | 설명 |
---|---|
8.0.0 | 공백("42 " )으로 끝나는 숫자 문자열은 이제 true 를 반환합니다. 이전에는 false 가 대신 반환되었습니다. |
Examples
예제 #1 is_numeric() 예제
<?php
$tests = array(
"42",
1337,
0x539,
02471,
0b10100111001,
1337e0,
"0x539",
"02471",
"0b10100111001",
"1337e0",
"not numeric",
array(),
9.1,
null,
'',
);
foreach ($tests as $element) {
if (is_numeric($element)) {
echo var_export($element, true) . " is numeric", PHP_EOL;
} else {
echo var_export($element, true) . " is NOT numeric", PHP_EOL;
}
}
?>
위의 예는 다음을 출력합니다.
'42' is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337.0 is numeric '0x539' is NOT numeric '02471' is numeric '0b10100111001' is NOT numeric '1337e0' is numeric 'not numeric' is NOT numeric array ( ) is NOT numeric 9.1 is numeric NULL is NOT numeric '' is NOT numeric
예제 #2 공백이 있는 is_numeric()
<?php
$tests = [
" 42",
"42 ",
"\u{A0}9001", // non-breaking space
"9001\u{A0}", // non-breaking space
];
foreach ($tests as $element) {
if (is_numeric($element)) {
echo var_export($element, true) . " is numeric", PHP_EOL;
} else {
echo var_export($element, true) . " is NOT numeric", PHP_EOL;
}
}
?>
PHP 8에서 위 예제의 출력:
' 42' is numeric '42 ' is numeric ' 9001' is NOT numeric '9001 ' is NOT numeric
PHP 7에서 위 예제의 출력:
' 42' is numeric '42 ' is NOT numeric ' 9001' is NOT numeric '9001 ' is NOT numeric
기타
- Numeric strings
- ctype_digit() - 숫자 확인
- is_bool() - 변수가 부울인지 여부를 찾습니다.
- is_null() - 변수가 null인지 여부를 찾습니다.
- is_float() - 변수의 유형이 float인지 여부를 찾습니다.
- is_int() - 변수의 유형이 정수인지 확인
- is_string() - 변수의 유형이 문자열인지 확인
- is_object() - 변수가 객체인지 여부를 찾습니다.
- is_array() - 변수가 배열인지 여부를 찾습니다.
- filter_var() - 지정된 필터로 변수를 필터링합니다.