Home  >  Article  >  Backend Development  >  Basic explanation of PHP: How to use the four operators in PHP

Basic explanation of PHP: How to use the four operators in PHP

不言
不言Original
2019-01-03 16:43:245246browse

The

operator is a symbol used to add or subtract programmatically. It can also be used to compare the size of two values ​​and determine whether they are the same or different. This article will introduce you to the operators in PHP. Instructions.

Basic explanation of PHP: How to use the four operators in PHP

There are four commonly used operators in php: arithmetic operators, assignment operators, comparison operators, logical operators

Below Let's take a look at the usage of these four operators respectively.

1. Arithmetic operators

The symbols of arithmetic operators are, -, *, /, %

Let’s look at the code Example

Addition:

<?php
// 加法
print("加法: 10 + 20 = ");
print(10 + 20);           // 10 + 20 的结果;
print("\n");

Subtraction:

// 减法
print("减法: 100 - 20 = ");
print(100 - 20);           // 100 - 20 的结果。
print("\n");

Multiplication:

// 乘法
print("乘法: 9 * 5 = ");
print(9 * 5);           // 9 * 5 的结果。
print("\n");

Division:

// 除法
print("除法: 100 / 10 = ");
print(100 / 10);           // 100 / 10 的结果。
print("\n");

Remainder:

// 余数
print("余数: 45 % 10 = ");
print(45 % 10);           // 45 % 10 的余数。
print("\n");
?>

The result of the operation is:

加法:10 + 20 = 30 
减法:100  -  20 = 80 
乘法:9 * 5 = 45 
除法:100/20 = 5 
余数:45%10 = 5

2. Assignment operator

The assignment operator is represented by "=".

变量=数值(或变量)

means assigning "value (or variable)" to "variable"

Let's look at the specific example

The code is as follows

<?php
$aisatsu = "php中文网";
print($aisatsu);
?>

Run The results are as follows:

php中文网

The string "php Chinese network" is assigned to $aisatsu.

The result of printing $aisatsu is displayed as "php Chinese website" through the print function. Of course, in addition to strings, values ​​and variables can also be assigned by assignment operators.

3. Comparison operators

Comparison operators are expressed as "==, > =, <=".

Let’s look at a specific example

The code is as follows

<?php
$x = 3;
if($x == 3) {
  print(&#39;$x等于3&#39;);
  print("\n");
}
if ($x >= 2) {
  print(&#39;$x大于或者等于2&#39;);
  print("\n");
}
if ($x <= 2) {
  print(&#39;$x小于或者等于2&#39;);
  print("\n");
}
?>

The running result is:

$x等于3
大于或者等于2
$x小于或者等于2

4. Logical operators

Logical operators are expressed as "and, or, &&, ||". "and" and "&&", "or" and "||" respectively have the same meaning.

and" and "&&" represent condition A and condition B; "or" and "||" represent condition A or condition B.

Let's look at specific examples

The code is as follows

<?php
$x = 3;
if($x >= 1 and $x <= 10){
  print(&#39;$ x大于或等于1且小于或等于10。&#39;);
  print("\n");
}
if($x >= 4 || $x <= 5 ){
  print(&#39;$ x大于或等于4或小于或等于5。&#39;);
  print("\n");
}
?>

The running result is:

$ x大于或等于1且小于或等于10。
$ x大于或等于4或小于或等于5。

Summary, the above is the entire content of this article, for more exciting content about php, you can pay attention to php Learn more on the Chinese website!!!

The above is the detailed content of Basic explanation of PHP: How to use the four operators in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn