1. Định nghĩa
- Hàm is_numeric() trong php dùng để kiểm tra một biến nào đó có phải là số hay chuỗi số không
2. Cú pháp
3. Tham sốMã nguồn PHP:is_numeric(mixed $value): bool
- $value là biến cần kiểm tra
4. Kết quả trả về
- true: nếu biến là số, hoặc là chuỗi số
- false: các trường hợp còn lại trả về false
5. Ví dụ:
Example #1 is_numeric() examplesMã nguồn PHP:<?php
$age = 23;
if (is_numeric($age)){
echo 'Biến age là số';
}else{
echo 'Biến age không phải là số';
}
// Kết quả: Biến age là số
- input
- output:Mã nguồn PHP:<?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;
}
}
?>
Example #2 is_numeric() with whitespaceMã nguồn PHP:'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
- input:
- output:Mã nguồn PHP:<?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;
}
}
?>
6. Tài liệu tham khảoMã nguồn PHP:' 42' is numeric
'42 ' is numeric
' 9001' is NOT numeric
'9001 ' is NOT numeric
https://www.php.net/manual/en/function.is-numeric.php



LinkBack URL
About LinkBacks







Trả lời kèm Trích dẫn
