Home > Backend Development > PHP Tutorial > Basic introduction to operators in php_PHP tutorial

Basic introduction to operators in php_PHP tutorial

WBOY
Release: 2016-07-13 17:14:54
Original
925 people have browsed it

The commonly used operators in PHP include arithmetic operators, assignment operators, comparison operators, logical operators, etc. Let me introduce the usage to my friends.

Arithmetic operators

The division operator always returns a floating point number. The only exception is if both operands are integers (or integers converted from strings) and are exactly divisible, in which case it returns an integer.

The operands of the modulo operator will be converted to integers (except for the decimal part) before operation.

Note: The result of modulo $a % $b when $a is negative is also negative.

Example:

The code is as follows Copy code
 代码如下 复制代码

/* tested under PHP 5.2.6-1 with Suhosin-Patch 0.9.6.2 (cli) on both i386 and amd64, Debian lenny/sid */
$a = 2863311530;
$b = 256;
$c = $a % $b;
echo "$c
n";
echo (2863311530 % 256)."
n"; /* directly with no variables, just to be sure */
?>

/* tested under PHP 5.2.6-1 with Suhosin-Patch 0.9.6.2 (cli) on both i386 and amd64, Debian lenny/sid */
$a = 2863311530;
$b = 256;
$c = $a % $b;
echo "$c
n";
echo (2863311530 % 256)."
n"; /* directly with no variables, just to be sure */
?>

运算符 说明 例子 结果
+ Addition x=2
x+2
4
- Subtraction x=2
5-x
3
* Multiplication x=4
x*5
20
/ Division 15/5
5/2
3
2.5
% Modulus (division remainder) 5%2
10%8
10%2
1
2
0
++ Increment x=5
x++
x=6
-- Decrement x=5
x--
x=4

Assignment operator

The basic assignment operator is "=". At first you may think it is "equal to", but it is not. It actually means assigning the value of the expression on the right to the operand on the left.

The value of the assignment expression is the assigned value. That is, the value of "$a = 3" is 3. Here are some tips:
 代码如下 复制代码

$a = ($b = 4) + 5; // $a 现在成了 9,而 $b 成了 4。

?>

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
The code is as follows Copy code

代码如下 复制代码

$a = 3;
$a += 5; // sets $a to 8, as if we had said: $a = $a + 5;
$b = "Hello ";
$b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";

?>

$a = ($b = 4) + 5; // $a is now 9 and $b is 4. ?> In addition to the basic assignment operators, there are "combining operators" suitable for all binary arithmetic, array set and string operators, which allow you to use its value in an expression and combine the expression's The result is assigned to it, for example:
The code is as follows Copy code
<🎜>$a = 3;
$a += 5; // sets $a to 8, as if we had said: $a = $a + 5;
$b = "Hello ";
$b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";<🎜> <🎜>?>

Note that the assignment operation copies the value of the original variable to the new variable (assignment by value), so changing one does not affect the other. This is also suitable for copying values ​​such as large arrays in very dense loops. You can also use reference assignment, using the $var = &$othervar; syntax. Reference assignment means that both variables point to the same data, and there is no copy of any data. See the citation description for more information about citations. In PHP 5, objects are always assigned by reference unless explicitly using the new clone keyword

运算符 说明 例子
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= x%=y x=x%y

Comparison operators

Comparison operators, as their name implies, allow comparison of two values. If you compare an integer and a string, the string is converted to an integer. If comparing two numeric strings, compare as integers. This rule also applies to switch statements.

The code is as follows Copy code
 代码如下 复制代码

var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("1" == "1e0"); // 1 == 1 -> true
switch ("a") {
case 0:
    echo "0";
    break;
case "a": // never reached because "a" is already matched with 0
    echo "a";
    break;
}
?>

var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("1" == "1e0"); // 1 == 1 -> true
switch ("a") {
case 0:
echo "0";
Break;
case "a": // never reached because "a" is already matched with 0
echo "a";
Break;
}
?>

运算符 说明 例子
== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
> is greater than 5>8 returns false
< is less than 5<8 returns true
>= is greater than or equal to 5>=8 returns false
<= is less than or equal to 5<=8 returns true
<🎜>

Logical operators

The reason why "AND" and "OR" have two different forms of operators is that the precedence of their operations is different (see operator precedence).

Example #1 Logical operator example

// The following foo() will not be called because they are "short-circuited" by the operator.
$a = (false && foo());
$b = (true || foo());
$c = (false and foo());
$d = (true or foo());
The code is as follows
代码如下 复制代码

// 下面的 foo() 不会被调用,因为它们被运算符“短路”了。
$a = (false && foo());
$b = (true || foo());
$c = (false and foo());
$d = (true or foo());

// "||" 的优先级比 "or" 高
$e = false || true; // $e 被赋值为 (false || true),结果为 true
$f = false or true; // $f 被赋值为 false [Altair注:"=" 的优先级比 "or" 高]
var_dump($e, $f);

// "&&" 的优先级比 "and" 高
$g = true && false; // $g 被赋值为 (true && false),结果为 false
$h = true and false; // $h 被赋值为 true [Altair注:"=" 的优先级比 "and" 高]
var_dump($g, $h);
?>
以上例程的输出类似于:

bool(true)
bool(false)
bool(false)
bool(true)

Copy code

运算符 说明 例子
&& and x=6
y=3

(x < 10 && y > 1) returns true

|| or x=6
y=3

(x==5 || y==5) returns false

! not x=6
y=3

!(x==y) returns true

// "||" has a higher priority than "or"
$e = false || true; // $e is assigned the value (false || true), and the result is true
$f = false or true; // $f is assigned false [Altair note: "=" has a higher priority than "or"]
var_dump($e, $f);

// "&&" has a higher priority than "and"
$g = true && false; // $g is assigned the value (true && false), and the result is false
$h = true and false; // $h is assigned true [Altair note: "=" has a higher priority than "and"]
var_dump($g, $h);
?>
The output of the above routine is similar to: bool(true)
bool(false)
bool(false)
bool(true) http://www.bkjia.com/PHPjc/628919.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628919.htmlTechArticleThe commonly used operators in PHP include arithmetic operators, assignment operators, comparison operators, and logical operations Talisman, etc. Now let me introduce to you how to use it. Arithmetic operators division...
Operator Description Examples
&& and x=6
y=3 (x < 10 && y > 1) returns true
|| or
x=6
y=3 (x==5 || y==5) returns false
! not x=6
y=3 !(x==y) returns true