# Basic Introduction to operators in php

Source: Internet
Author: User

The division operator always returns a floating point number. Only in the following cases: both operands are integers (or integers converted into strings) and can be fully divided. In this case, an integer is returned.
The operands of the modulo operator are converted to integers (excluding decimal digits) before the operation ).
Note: the result of modulo \$ a % \$ B when \$ a is a negative value is also a negative value.
Example:

The code is as follows:
Copy code

<? Php
/* 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 <br/> n ";
Echo (2863311530% 256). "<br/> n";/* directly with no variables, just to be sure */
?>

Operator
Description
Example
Result

+
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

Value assignment operator
The basic assignment operator is "= ". At first, we may think it is "equal", but it is not. It actually means assigning the value of the right expression to the number of operations on the left.
The value of the value assignment expression is the value assigned. That is to say, the value of "\$ a = 3" is 3. In this way, you can do some tips:

The code is as follows:
Copy code

<? Php
\$ A = (\$ B = 4) + 5; // \$ a is now 9, while \$ B is 4.
?>

In addition to the basic value assignment operators, there are also "combined operators" suitable for all binary arithmetic, array sets, and string operators ", in this way, you can use its value in an expression and assign the result of the expression to it. For example:

The code is as follows:
Copy code

<? Php
\$ 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 value assignment operation copies the value of the original variable to the new variable (pass the value assignment), so changing one does not affect the other. This is also suitable for copying some values such as large arrays in intensive loops. You can also assign values by reference using the \$ var = & \$ othervar; syntax. Referencing a value assignment means that both variables point to the same data without any data copies. For more information about references, see references. In PHP 5, objects are always assigned by reference unless the new clone keyword is explicitly used.

Operator
Description
Example

=
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 names imply, allow comparison of two values. If you compare an integer with a string, the string is converted to an integer. Compare two numeric strings as integers. This rule also applies to switch statements.

The code is as follows:
Copy code

<? Php
Var_dump (0 = "a"); // 0 = 0-> true
Var_dump ("1" = "01"); // 1 = 1-> true
Var_dump ("1" = "1e0"); // 1 = 1-> true
Switch (""){
Case 0:
Echo "0 ";
Break;
Case "a": // never reached because "a" is already matched with 0
Echo "";
Break;
}
?>

Operator
Description
Example

=
Is equal
5 = 8 returns false

! =
Is not equal
5! = 8 returns true

>
Is greater
5> 8 returns false

<
Is less
5 <8 returns true

> =
Is greater than or equal
5> = 8 returns false

<=
Is less than or equal
5 <= 8 returns true

Logical operators
"And" and "or" have two different operators because they have different operation priorities (see operator priorities ).
Example #1 Example of logical operators

The code is as follows:
Copy code

<? Php
// 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 ());
// "|" Has a higher priority than "or"
\$ E = false | true; // \$ e is assigned (false | true) and returns true.
\$ F = false or true; // \$ f is assigned to 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 to (true & false) and the result is false.
\$ H = true and false; // \$ h is assigned true [Altair note: "=" priority is higher than "and"]
Var_dump (\$ g, \$ h );
?>
The output of the preceding routine is similar:
Bool (true)
Bool (false)
Bool (false)
Bool (true)

Operator
Description
Example

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

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.