Hướng dẫn dùng arithemetic trong PHP

Lệnh expr trong Linux đánh giá một biểu thức nhất định và hiển thị đầu ra tương ứng của nó. Lệnh expr dùng để:

  • Thực hiện các phép toán cơ bản như cộng, trừ, nhân, chia và mô đun trên số nguyên.
  • Đánh giá các biểu thức chính quy, các hoạt động chuỗi như chuỗi con, độ dài của chuỗi, v.v.

Cú pháp lệnh expr

expr [arithmetic expression]

Chú thích tham số lệnh expr:

  • [arithmetic expression]: Là biểu thức toán học.

Các tùy chọn lệnh expr:

Tùy chọn --version sử dụng để hiển thị thông tin phiên bản expr:

expr (GNU coreutils) 8.30 Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Mike Parker, James Youngman, and Paul Eggert.

Tùy chọn --help dùng để hiển thị thông báo trợ giúp lệnh expr

Usage: expr EXPRESSION or: expr OPTION --help display this help and exit --version output version information and exit Print the value of EXPRESSION to standard output. A blank line below separates increasing precedence groups. EXPRESSION may be: ARG1 | ARG2 ARG1 if it is neither null nor 0, otherwise ARG2 ARG1 & ARG2 ARG1 if neither argument is null or 0, otherwise 0 ARG1 < ARG2 ARG1 is less than ARG2 ARG1 <= ARG2 ARG1 is less than or equal to ARG2 ARG1 = ARG2 ARG1 is equal to ARG2 ARG1 != ARG2 ARG1 is unequal to ARG2 ARG1 >= ARG2 ARG1 is greater than or equal to ARG2 ARG1 > ARG2 ARG1 is greater than ARG2 ARG1 + ARG2 arithmetic sum of ARG1 and ARG2 ARG1 - ARG2 arithmetic difference of ARG1 and ARG2 ARG1 * ARG2 arithmetic product of ARG1 and ARG2 ARG1 / ARG2 arithmetic quotient of ARG1 divided by ARG2 ARG1 % ARG2 arithmetic remainder of ARG1 divided by ARG2 STRING : REGEXP anchored pattern match of REGEXP in STRING match STRING REGEXP same as STRING : REGEXP substr STRING POS LENGTH substring of STRING, POS counted from 1 index STRING CHARS index in STRING where any CHARS is found, or 0 length STRING length of STRING + TOKEN interpret TOKEN as a string, even if it is a keyword like 'match' or an operator like '/' ( EXPRESSION ) value of EXPRESSION . . .

Sử dụng lệnh expr cho các phép toán số học cơ bản

Phép cộng

Chú thích tham số lệnh expr:

  • a: Là số nguyên thứ nhất.
  • b: Là số nguyên thứ hai.
  • +: Là phép cộng hai số nguyên.

Ví dụ minh họa:

Phép trừ

Chú thích tham số lệnh expr:

  • a: Là số nguyên thứ nhất.
  • b: Là số nguyên thứ hai.
  • expr (GNU coreutils) 8.30 Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Mike Parker, James Youngman, and Paul Eggert. 2: Là phép trừ hai số nguyên.

Ví dụ minh họa:

Phép nhân

Chú thích tham số lệnh expr:

  • a: Là số nguyên thứ nhất.
  • b: Là số nguyên thứ hai.
  • expr (GNU coreutils) 8.30 Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Mike Parker, James Youngman, and Paul Eggert. 5: Là phép nhân hai số nguyên.

Ví dụ minh họa:

Phép chia

Chú thích tham số lệnh expr:

  • a: Là số nguyên thứ nhất.
  • b: Là số nguyên thứ hai.
  • expr (GNU coreutils) 8.30 Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Mike Parker, James Youngman, and Paul Eggert. 8: Là phép chia hai số nguyên.

Ví dụ minh họa:

Lưu ý: Chỉ áp dụng cho các phép chia hết để có kết quả chính xác nhất. Vì lệnh expr luôn trả về một số nguyên.

Phép chia lấy dư

Chú thích tham số lệnh expr:

  • a: Là số nguyên thứ nhất.
  • b: Là số nguyên thứ hai.
  • Usage: expr EXPRESSION or: expr OPTION --help display this help and exit --version output version information and exit Print the value of EXPRESSION to standard output. A blank line below separates increasing precedence groups. EXPRESSION may be: ARG1 | ARG2 ARG1 if it is neither null nor 0, otherwise ARG2 ARG1 & ARG2 ARG1 if neither argument is null or 0, otherwise 0 ARG1 < ARG2 ARG1 is less than ARG2 ARG1 <= ARG2 ARG1 is less than or equal to ARG2 ARG1 = ARG2 ARG1 is equal to ARG2 ARG1 != ARG2 ARG1 is unequal to ARG2 ARG1 >= ARG2 ARG1 is greater than or equal to ARG2 ARG1 > ARG2 ARG1 is greater than ARG2 ARG1 + ARG2 arithmetic sum of ARG1 and ARG2 ARG1 - ARG2 arithmetic difference of ARG1 and ARG2 ARG1 * ARG2 arithmetic product of ARG1 and ARG2 ARG1 / ARG2 arithmetic quotient of ARG1 divided by ARG2 ARG1 % ARG2 arithmetic remainder of ARG1 divided by ARG2 STRING : REGEXP anchored pattern match of REGEXP in STRING match STRING REGEXP same as STRING : REGEXP substr STRING POS LENGTH substring of STRING, POS counted from 1 index STRING CHARS index in STRING where any CHARS is found, or 0 length STRING length of STRING + TOKEN interpret TOKEN as a string, even if it is a keyword like 'match' or an operator like '/' ( EXPRESSION ) value of EXPRESSION . . . 1: Là phép chia lấy phần dư hai số nguyên.

Ví dụ minh họa:

Đếm độ dài chuỗi

Để đếm độ dài một chuỗi trên Linux chúng ta có thể dùng lệnh sau:

Chú thích tham số lệnh expr:

  • Usage: expr EXPRESSION or: expr OPTION --help display this help and exit --version output version information and exit Print the value of EXPRESSION to standard output. A blank line below separates increasing precedence groups. EXPRESSION may be: ARG1 | ARG2 ARG1 if it is neither null nor 0, otherwise ARG2 ARG1 & ARG2 ARG1 if neither argument is null or 0, otherwise 0 ARG1 < ARG2 ARG1 is less than ARG2 ARG1 <= ARG2 ARG1 is less than or equal to ARG2 ARG1 = ARG2 ARG1 is equal to ARG2 ARG1 != ARG2 ARG1 is unequal to ARG2 ARG1 >= ARG2 ARG1 is greater than or equal to ARG2 ARG1 > ARG2 ARG1 is greater than ARG2 ARG1 + ARG2 arithmetic sum of ARG1 and ARG2 ARG1 - ARG2 arithmetic difference of ARG1 and ARG2 ARG1 * ARG2 arithmetic product of ARG1 and ARG2 ARG1 / ARG2 arithmetic quotient of ARG1 divided by ARG2 ARG1 % ARG2 arithmetic remainder of ARG1 divided by ARG2 STRING : REGEXP anchored pattern match of REGEXP in STRING match STRING REGEXP same as STRING : REGEXP substr STRING POS LENGTH substring of STRING, POS counted from 1 index STRING CHARS index in STRING where any CHARS is found, or 0 length STRING length of STRING + TOKEN interpret TOKEN as a string, even if it is a keyword like 'match' or an operator like '/' ( EXPRESSION ) value of EXPRESSION . . . 2: Là độ dài chuỗi kí tự a. Ví dụ minh họa: Kiểm tra độ dài chuỗi Usage: expr EXPRESSION or: expr OPTION --help display this help and exit --version output version information and exit Print the value of EXPRESSION to standard output. A blank line below separates increasing precedence groups. EXPRESSION may be: ARG1 | ARG2 ARG1 if it is neither null nor 0, otherwise ARG2 ARG1 & ARG2 ARG1 if neither argument is null or 0, otherwise 0 ARG1 < ARG2 ARG1 is less than ARG2 ARG1 <= ARG2 ARG1 is less than or equal to ARG2 ARG1 = ARG2 ARG1 is equal to ARG2 ARG1 != ARG2 ARG1 is unequal to ARG2 ARG1 >= ARG2 ARG1 is greater than or equal to ARG2 ARG1 > ARG2 ARG1 is greater than ARG2 ARG1 + ARG2 arithmetic sum of ARG1 and ARG2 ARG1 - ARG2 arithmetic difference of ARG1 and ARG2 ARG1 * ARG2 arithmetic product of ARG1 and ARG2 ARG1 / ARG2 arithmetic quotient of ARG1 divided by ARG2 ARG1 % ARG2 arithmetic remainder of ARG1 divided by ARG2 STRING : REGEXP anchored pattern match of REGEXP in STRING match STRING REGEXP same as STRING : REGEXP substr STRING POS LENGTH substring of STRING, POS counted from 1 index STRING CHARS index in STRING where any CHARS is found, or 0 length STRING length of STRING + TOKEN interpret TOKEN as a string, even if it is a keyword like 'match' or an operator like '/' ( EXPRESSION ) value of EXPRESSION . . . 4 chúng ta dùng lệnh sau:

Kiểm tra chỉ số của kí tự trong một chuỗi

Chú thích tham số lệnh expr:

  • Usage: expr EXPRESSION or: expr OPTION --help display this help and exit --version output version information and exit Print the value of EXPRESSION to standard output. A blank line below separates increasing precedence groups. EXPRESSION may be: ARG1 | ARG2 ARG1 if it is neither null nor 0, otherwise ARG2 ARG1 & ARG2 ARG1 if neither argument is null or 0, otherwise 0 ARG1 < ARG2 ARG1 is less than ARG2 ARG1 <= ARG2 ARG1 is less than or equal to ARG2 ARG1 = ARG2 ARG1 is equal to ARG2 ARG1 != ARG2 ARG1 is unequal to ARG2 ARG1 >= ARG2 ARG1 is greater than or equal to ARG2 ARG1 > ARG2 ARG1 is greater than ARG2 ARG1 + ARG2 arithmetic sum of ARG1 and ARG2 ARG1 - ARG2 arithmetic difference of ARG1 and ARG2 ARG1 * ARG2 arithmetic product of ARG1 and ARG2 ARG1 / ARG2 arithmetic quotient of ARG1 divided by ARG2 ARG1 % ARG2 arithmetic remainder of ARG1 divided by ARG2 STRING : REGEXP anchored pattern match of REGEXP in STRING match STRING REGEXP same as STRING : REGEXP substr STRING POS LENGTH substring of STRING, POS counted from 1 index STRING CHARS index in STRING where any CHARS is found, or 0 length STRING length of STRING + TOKEN interpret TOKEN as a string, even if it is a keyword like 'match' or an operator like '/' ( EXPRESSION ) value of EXPRESSION . . . 5: Kiểm tra chỉ số của kí tự trong chuỗi a.
  • b: Là kí tự cần kiểm tra chỉ số trong chuỗi a. Ví dụ minh họa: Kiểm tra chỉ số của kí tự Usage: expr EXPRESSION or: expr OPTION --help display this help and exit --version output version information and exit Print the value of EXPRESSION to standard output. A blank line below separates increasing precedence groups. EXPRESSION may be: ARG1 | ARG2 ARG1 if it is neither null nor 0, otherwise ARG2 ARG1 & ARG2 ARG1 if neither argument is null or 0, otherwise 0 ARG1 < ARG2 ARG1 is less than ARG2 ARG1 <= ARG2 ARG1 is less than or equal to ARG2 ARG1 = ARG2 ARG1 is equal to ARG2 ARG1 != ARG2 ARG1 is unequal to ARG2 ARG1 >= ARG2 ARG1 is greater than or equal to ARG2 ARG1 > ARG2 ARG1 is greater than ARG2 ARG1 + ARG2 arithmetic sum of ARG1 and ARG2 ARG1 - ARG2 arithmetic difference of ARG1 and ARG2 ARG1 * ARG2 arithmetic product of ARG1 and ARG2 ARG1 / ARG2 arithmetic quotient of ARG1 divided by ARG2 ARG1 % ARG2 arithmetic remainder of ARG1 divided by ARG2 STRING : REGEXP anchored pattern match of REGEXP in STRING match STRING REGEXP same as STRING : REGEXP substr STRING POS LENGTH substring of STRING, POS counted from 1 index STRING CHARS index in STRING where any CHARS is found, or 0 length STRING length of STRING + TOKEN interpret TOKEN as a string, even if it is a keyword like 'match' or an operator like '/' ( EXPRESSION ) value of EXPRESSION . . . 9 trong chuỗi Usage: expr EXPRESSION or: expr OPTION --help display this help and exit --version output version information and exit Print the value of EXPRESSION to standard output. A blank line below separates increasing precedence groups. EXPRESSION may be: ARG1 | ARG2 ARG1 if it is neither null nor 0, otherwise ARG2 ARG1 & ARG2 ARG1 if neither argument is null or 0, otherwise 0 ARG1 < ARG2 ARG1 is less than ARG2 ARG1 <= ARG2 ARG1 is less than or equal to ARG2 ARG1 = ARG2 ARG1 is equal to ARG2 ARG1 != ARG2 ARG1 is unequal to ARG2 ARG1 >= ARG2 ARG1 is greater than or equal to ARG2 ARG1 > ARG2 ARG1 is greater than ARG2 ARG1 + ARG2 arithmetic sum of ARG1 and ARG2 ARG1 - ARG2 arithmetic difference of ARG1 and ARG2 ARG1 * ARG2 arithmetic product of ARG1 and ARG2 ARG1 / ARG2 arithmetic quotient of ARG1 divided by ARG2 ARG1 % ARG2 arithmetic remainder of ARG1 divided by ARG2 STRING : REGEXP anchored pattern match of REGEXP in STRING match STRING REGEXP same as STRING : REGEXP substr STRING POS LENGTH substring of STRING, POS counted from 1 index STRING CHARS index in STRING where any CHARS is found, or 0 length STRING length of STRING + TOKEN interpret TOKEN as a string, even if it is a keyword like 'match' or an operator like '/' ( EXPRESSION ) value of EXPRESSION . . . 4 chúng ta dùng lệnh sau:

So sánh hai biểu thức

Sử dụng lệnh “expr”, chúng ta cũng có thể so sánh hai biểu thức. Để so sánh các biểu thức, hãy sử dụng các toán tử logic như “=,>,! =”.

  • Nếu điều kiện hợp lệ, nó sẽ hiển thị expr 123host.vn : 123host 1.
  • Nếu điều kiện không hợp lệ, nó sẽ hiển thị expr 123host.vn : 123host 2 trong đầu ra.

Chú thích tham số lênh expr:

  • a: Là số nguyên thứ nhất.
  • b: Là số nguyên thứ hai.
  • expr 123host.vn : 123host 5: Là phép so sánh bằng. Ví dụ minh họa: So sánh hai số nguyên 30 và 40

So sánh không bằng

Chú thích tham số lệnh expr:

  • expr 123host.vn : 123host 6: Là các số nguyên cần so sánh.
  • expr 123host.vn : 123host 7: Là phép so sánh không bằng.

Ví dụ minh họa: So sánh không bằng hai số 20 và 30 bằng lệnh sau:

So sánh lớn hơn, bé hơn

Để so sánh lớn hơn chúng ta dùng lệnh sau:

Để so sánh bé hơn chúng ta dùng lệnh sau:

Chú thích tham số lệnh expr:

  • a: Là số nguyên thứ nhất.
  • b: Là số nguyên thứ hai.
  • [arithmetic expression]0: Là phép so sánh lớn hơn.
  • [arithmetic expression]1: Là phép so sánh bé hơn.

Ví dụ minh họa: So sánh lớn hơn, bé hơn hai số nguyên 30 và 40 chúng ta dùng lệnh sau:

So khớp số kí tự của hai chuỗi

Giả sử, chúng ta có hai chuỗi và muốn khớp số ký tựcủa hai chuỗi với nhau. Hãy thực hiện theo cú pháp:

Chú thích tham số lệnh expr:

  • [arithmetic expression]2: Là nội dung chuỗi thứ nhất.
  • [arithmetic expression]3: Là nội dung chuỗi thứ hai.

Ví dụ minh họa: Đếm số kí tự khớp nhau của hai chuỗi Usage: expr EXPRESSION or: expr OPTION --help display this help and exit --version output version information and exit Print the value of EXPRESSION to standard output. A blank line below separates increasing precedence groups. EXPRESSION may be: ARG1 | ARG2 ARG1 if it is neither null nor 0, otherwise ARG2 ARG1 & ARG2 ARG1 if neither argument is null or 0, otherwise 0 ARG1 < ARG2 ARG1 is less than ARG2 ARG1 <= ARG2 ARG1 is less than or equal to ARG2 ARG1 = ARG2 ARG1 is equal to ARG2 ARG1 != ARG2 ARG1 is unequal to ARG2 ARG1 >= ARG2 ARG1 is greater than or equal to ARG2 ARG1 > ARG2 ARG1 is greater than ARG2 ARG1 + ARG2 arithmetic sum of ARG1 and ARG2 ARG1 - ARG2 arithmetic difference of ARG1 and ARG2 ARG1 * ARG2 arithmetic product of ARG1 and ARG2 ARG1 / ARG2 arithmetic quotient of ARG1 divided by ARG2 ARG1 % ARG2 arithmetic remainder of ARG1 divided by ARG2 STRING : REGEXP anchored pattern match of REGEXP in STRING match STRING REGEXP same as STRING : REGEXP substr STRING POS LENGTH substring of STRING, POS counted from 1 index STRING CHARS index in STRING where any CHARS is found, or 0 length STRING length of STRING + TOKEN interpret TOKEN as a string, even if it is a keyword like 'match' or an operator like '/' ( EXPRESSION ) value of EXPRESSION . . . 4 và [arithmetic expression]5 chúng ta dùng lệnh như sau:

expr 123host.vn : 123host

Kết luận

Lệnh expr đánh giá và hiển thị đầu ra tiêu chuẩn của một biểu thức nhất định. Mỗi biểu thức riêng biệt được coi là một tuyên bố. Biểu thức số nguyên, chuỗi và biểu thức chính quy được sử dụng với lệnh "expr". Chúng ta đã đề cập đến nhiều ví dụ về lệnh expr trong hướng dẫn này. Hi vọng qua bài viết sẽ giúp bạn hiểu hơn về lệnh expr trên Linux.

Chủ đề