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
- Mã: Chọn tất cả
- is_numeric(mixed $value): bool
3. Tham số
- $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ụ:
- Mã: Chọn tất cả
- <?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ố
Example #1 is_numeric() examples
- input
- Mã: Chọn tất cả
- <?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;
 }
 }
 ?>
- output:
- Mã: Chọn tất cả
- '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
Example #2 is_numeric() with whitespace
- input:
- Mã: Chọn tất cả
- <?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;
 }
 }
 ?>
- output:
- Mã: Chọn tất cả
- ' 42' is numeric
 '42 ' is numeric
 ' 9001' is NOT numeric
 '9001 ' is NOT numeric
6. Tài liệu tham khảo
https://www.php.net/manual/en/function.is-numeric.php


 gửi bởi
 gửi bởi 







 
	


