Hướng dẫn is numeric validation in php? - là xác thực số trong php?

is_numeric() kiểm tra xem một giá trị có phải là một số hay không. Nó không nhất thiết phải là một số nguyên mặc dù - nó có thể là một số thập phân hoặc một số trong ký hiệu khoa học.

Ví dụ preg_match() mà bạn chỉ đưa ra chỉ kiểm tra xem giá trị có chứa các chữ số từ 0 đến chín; Bất kỳ số lượng trong số họ, và trong bất kỳ chuỗi.

Lưu ý rằng biểu thức thông thường mà bạn đã đưa ra cũng không phải là trình kiểm tra số nguyên hoàn hảo, cách bạn đã viết nó. Nó không cho phép tiêu cực; Nó cho phép một chuỗi có độ dài bằng không (nghĩa là không có chữ số nào, có lẽ không nên hợp lệ?), Và nó cho phép số có số 0 không có số 0, một lần nữa có thể không được dự định.

[EDIT]

Theo bình luận của bạn, một biểu thức thông thường tốt hơn có thể trông như thế này:

/^[1-9][0-9]*$/

Điều này buộc chữ số đầu tiên chỉ là từ 1 đến 9, vì vậy bạn không thể có số không hàng đầu. Nó cũng buộc nó phải dài ít nhất một chữ số, do đó giải quyết vấn đề chuỗi có độ dài bằng không.

Bạn không lo lắng về những tiêu cực, vì vậy đó không phải là vấn đề.

Bạn có thể muốn hạn chế số lượng các chữ số, bởi vì mọi thứ đứng, nó sẽ cho phép các chuỗi quá lớn để được lưu trữ dưới dạng số nguyên. Để hạn chế điều này, bạn sẽ thay đổi ngôi sao thành hạn chế độ dài như vậy:

/^[1-9][0-9]{0,15}$/

Điều này sẽ cho phép chuỗi dài từ 1 đến 16 chữ số (tức là chữ số đầu tiên cộng với 0-15 chữ số nữa). Hãy thoải mái điều chỉnh các con số trong niềng răng xoăn phù hợp với nhu cầu của riêng bạn. Nếu bạn muốn một chuỗi độ dài cố định, thì bạn chỉ cần chỉ định một số trong niềng răng.

Hy vọng điều đó sẽ giúp.

(Php 4, Php 5, Php 7, Php 8)

is_numeric - tìm thấy một biến là số hay chuỗi số Finds whether a variable is a number or a numeric string

Sự mô tả

is_numeric (hỗn hợp $value): bool(mixed $value): bool

Thông số

________số 8

Biến được đánh giá.

Trả về giá trị

Trả về true nếu value là một số hoặc chuỗi số,

/^[1-9][0-9]{0,15}$/
1 nếu không.true if value is a number or a numeric string,
/^[1-9][0-9]{0,15}$/
1
otherwise.

Thay đổi

Phiên bảnSự mô tả
8.0.0 is_numeric (hỗn hợp $value): booltrue. Previously,
/^[1-9][0-9]{0,15}$/
1
was return instead.

Thông số

________số 8is_numeric() examples

/^[1-9][0-9]{0,15}$/
5

/^[1-9][0-9]{0,15}$/
6

/^[1-9][0-9]{0,15}$/
7

Biến được đánh giá.

'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

Trả về giá trịis_numeric() with whitespace

/^[1-9][0-9]{0,15}$/
8

/^[1-9][0-9]{0,15}$/
6

/^[1-9][0-9]{0,15}$/
7

Trả về true nếu value là một số hoặc chuỗi số,

/^[1-9][0-9]{0,15}$/
1 nếu không.

' 42' is numeric
'42 ' is numeric
' 9001' is NOT numeric
'9001 ' is NOT numeric

Thay đổi

' 42' is numeric
'42 ' is NOT numeric
' 9001' is NOT numeric
'9001 ' is NOT numeric

Phiên bản

  • Các chuỗi số kết thúc bằng khoảng trắng (
    /^[1-9][0-9]{0,15}$/
    
    2) bây giờ sẽ trả về true. Trước đây,
    /^[1-9][0-9]{0,15}$/
    
    1 đã trở lại thay thế.
  • Ví dụ
  • Ví dụ #1 is_numeric () ví dụ
  • Ví dụ trên sẽ xuất ra:
  • Ví dụ #2 is_numeric () với khoảng trắng
  • Đầu ra của ví dụ trên trong Php 8:
  • Đầu ra của ví dụ trên trong Php 7:
  • Xem thêm
  • Chuỗi số
  • ctype_digit () - Kiểm tra (các) ký tự số

is_bool () - Tìm hiểu xem một biến có phải là boolean

is_null () - tìm thấy một biến có phải là null

'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
1

'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

'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
3

'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
4

'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
5

'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
6

'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
7

is_float () - Tìm liệu loại biến có nổi không

is_int () - Tìm loại biến có số nguyên không

'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
8

is_String () - Tìm loại biến là chuỗi

is_Object () - Tìm xem một biến có phải là một đối tượng

'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
9

' 42' is numeric
'42 ' is numeric
' 9001' is NOT numeric
'9001 ' is NOT numeric
0

' 42' is numeric
'42 ' is numeric
' 9001' is NOT numeric
'9001 ' is NOT numeric
1

'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
7

is_array () - Tìm xem một biến có phải là một mảng không

Filter_var () - Lọc một biến có bộ lọc được chỉ định

' 42' is numeric
'42 ' is numeric
' 9001' is NOT numeric
'9001 ' is NOT numeric
3

' 42' is numeric
'42 ' is numeric
' 9001' is NOT numeric
'9001 ' is NOT numeric
4

' 42' is numeric
'42 ' is numeric
' 9001' is NOT numeric
'9001 ' is NOT numeric
5

' 42' is numeric
'42 ' is numeric
' 9001' is NOT numeric
'9001 ' is NOT numeric
6

' 42' is numeric
'42 ' is numeric
' 9001' is NOT numeric
'9001 ' is NOT numeric
7

'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
7

Sobolanx tại gmail dot com ¶

11 năm trước

' 42' is numeric
'42 ' is numeric
' 9001' is NOT numeric
'9001 ' is NOT numeric
9

' 42' is numeric
'42 ' is NOT numeric
' 9001' is NOT numeric
'9001 ' is NOT numeric
0

' 42' is numeric
'42 ' is NOT numeric
' 9001' is NOT numeric
'9001 ' is NOT numeric
1

'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
7

Là mảng số PHP?

Các mảng số cho phép chúng tôi lưu trữ nhiều giá trị của cùng một loại dữ liệu trong một biến mà không phải tạo các biến riêng biệt cho mỗi giá trị. Các giá trị này sau đó có thể được truy cập bằng cách sử dụng một chỉ mục trong trường hợp các mảng số luôn là một số.. These values can then be accessed using an index which in case of numeric arrays is always a number.

Chuỗi số trong PHP là gì?

Chuỗi số ¶ Một chuỗi PHP được coi là số nếu nó có thể được hiểu là INT hoặc FLOAT. Chính thức kể từ Php 8.0.0: khoảng trắng \ S* lnum [0-9]+ dnum ([0-9]*) [\. ] {Lnum}) | .if it can be interpreted as an int or a float. Formally as of PHP 8.0.0: WHITESPACES \s* LNUM [0-9]+ DNUM ([0-9]*)[\. ]{LNUM}) | ({LNUM}[\. ][0-9]*) EXPONENT_DNUM (({LNUM} | {DNUM}) [eE][+-]?

Làm thế nào để bạn kiểm tra xem một số là số nguyên hoặc nổi trong PHP?

Hàm is_float () kiểm tra xem một biến có phải là float hay không.Hàm này trả về true (1) nếu biến là loại float, nếu không nó sẽ trả về sai.is_float() function checks whether a variable is of type float or not. This function returns true (1) if the variable is of type float, otherwise it returns false.

Làm thế nào giá trị kiểm tra là thập phân hay không trong PHP?

Hàm is_numeric () trong ngôn ngữ lập trình PHP được sử dụng để đánh giá xem một giá trị là số hoặc chuỗi số.Các chuỗi số chứa bất kỳ số lượng chữ số nào, các dấu hiệu tùy chọn như + hoặc -, số thập phân tùy chọn và theo cấp số nhân tùy chọn.Do đó, +234.5E6 là một chuỗi số hợp lệ.is_numeric() function in the PHP programming language is used to evaluate whether a value is a number or numeric string. Numeric strings contain any number of digits, optional signs such as + or -, an optional decimal, and an optional exponential. Therefore, +234.5e6 is a valid numeric string.