PHP basic tutorial 3 operator

Source: Internet
Author: User
This section describes how to increase or decrease arithmetic operators. assign values to operators. string operators. Comparison operators. bit operators. logical operators. ternary operators. Incorrect control operators.

Content described in this section
  • Arithmetic operators

  • Increment and decrease operators

  • Value assignment operator

  • String operators

  • Comparison Operators

  • Bitwise operators

  • Logical operators

  • Ternary operators

  • Error Control Operator

Preface

The PHP operator is a skill that must be mastered in the foundation of php. it is a bridge for php Data Processing. operators are used for various data operations, such as addition, subtraction, multiplication, and division, AND, OR.

Arithmetic operators

Arithmetic operator: refers to the symbol used to process the four arithmetic operations. this is the simplest and most commonly used symbol, especially the number processing, and almost always uses the arithmetic operator number.

Arithmetic operator classification:

-$ A: returns the inverse value of $.

$ A =-1; echo-$ a; ----- result ----- 1

$ A + $ B addition $ a and $ B's and

$ A = 12; $ B = 12; $ res = $ a + $ B; echo $ res; ----- result ----- 24

$ A-$ B subtraction $ a and $ B

$ A = 12; $ B = 6; $ res = $ a-$ B; echo $ res; ----- result ----- 6

$ A * $ B multiplication $ the product of a and $ B

$ A = 2; $ B = 3; $ res = $ a * $ B; echo $ res; ----- result ----- 6

$ A/$ B Division $ a and $ B operator

$ A = 12; $ B = 6; $ res = $ a/$ B; echo $ res; ----- result ----- 2

$ A % $ B modulo the remainder of $ a and $ B;

$ A = 3; $ B = 2; $ res = $ a % $ B; echo $ res; ----- result ----- 1
Increment and decrease operators
  • + $ A: add one to the value of $ a, and then return $ a. This is the first addition.

  • $ A ++ returns $ a, and then adds the value of $ a to the value. this is the add-on.

  • -The value of $ a is reduced by one, and then $ a is returned.

  • $ A-$ a is returned, and then the value of $ a is reduced by one.

++ $

$ A = 1; $ B = ++ $ a; // first ++, equivalent to $ a = $ a + 1; $ B = $; echo $ B.'
'. $ A; ----- result ----- $ B = 2 $ a = 2

$ A ++

$ A = 1; $ B = $ a ++; // assign a value first. The operation is equivalent to $ B = $ a; $ a = $ a + 1; echo $ B.'
'. $ A; ----- result ----- $ B = 1 $ a = 2

The reduction is the same as the above.

Value assignment operator

The basic value assignment operator is "=", which actually means that the value of the right expression is assigned to the number of operations on the left.

$a = 1;$b = 'string';

Also+ =,-=,/=And so on

$ A = 1; $ a + = 2; // equivalent to $ a = $ a + 2; echo $ a; ----- result ----- 3
String operators

There are two strings. The operator is ("."), which returns the string after the left and right parameters are connected.

$ A = 1; $ B = 2; echo '$ a = '. $. '$ B = '. $ B; ----- result ----- $ a = 1 $ B = 2;
Comparison Operators

The comparison operator compares two variables. if the values are the same and the data types are the same, the comparison result is mostly Boolean.

Common comparison operators are:

  • $ A = $ B is equal to true. when $ a = $ B, only the values are equal and the types are not equal.

  • $ A ===$ B all equal to true, if $ a is equal to $ B, and their types are the same.

  • $! = $ B is not equal to ture. if the values of the two variables are not equal, true is returned;

  • $ A <> $ B is not equal to true. in the preceding case, another method is used. if $ a is not equal to $ B after type conversion.

  • $! = $ B is not all equal to true. if $ a is not equal to $ B or their types are different, true is returned when either of the two variables is not equal. (value and type)

  • $ A <$ B small and TRUE, if $ a is strictly less than $ B.

  • $ A> $ B is greater than TRUE if $ a is strictly greater than $ B.

  • $ A <= $ B is less than or equal to TRUE, if $ a is less than or equal to $ B.

  • $ A >=$ B is greater than or equal to TRUE if $ a is greater than or equal to $ B.

    $ A = 2; $ B = '2'; var_dump ($ a = $ B); // determines whether the two values are equal. type conversion is performed during comparison. ----- Result ----- bool (true) var_dump ($ a ===$ B); // determines whether the value and type are equal. if the comparison is full, no type conversion is performed. ----- Result ----- bool (false)
Bitwise operators

The bitwise operator allows the specified bitwise in the integerBinary.

Before that, let's talk about binary.

Introduction to binary

All memory errors in the computer are stored in binary format. The bitwise operation is to directly operate the binary bit of the integer in the memory.

What is binary: binary data is expressed in numbers 0 and 1. Its base is 2, the carry rule is "every two into one", and the borrow rule is "borrow one as two ".

Convert decimal to binary: "Divide by 2 to take the remainder, sort in reverse order" (except for the two take the remainder method)

Quotient remainder 3/2 = 1 ..... 11/2 = 0 ..... 1 Binary (in reverse order): 1112/2 = 6 .... 06/2 = 3 ..... 03/2 = 1 ..... 11/2 = 0 ..... 1 Binary: 110015/2 = 7 .... 17/2 = 3 ..... 13/2 = 1 ..... 11/2 = 0 ..... 1 Binary: 1111

Convert binary to decimal: "Sum by right"

1111 = 1 * 20 + 1 * 21 + 1 * 22 + 1 * 23 = 1510 = 0 * 20 + 1 * 21 = 2

There are three forms of expression for the symbol bit in the computer:

  • Original Code

  • Reverse Code

  • Complement

During bitwise operations, the bitwise operations are all converted into complement operations.

Positive numberThe binary representation of is a positive number at the beginning of the binary.
The original codes, backcodes, and supplementary codes of positive numbers are the same (three-in-One codes)

NegativeThe binary representation of is a negative number at the beginning of the binary.
The original code of a negative number is the binary number obtained using the formula. the anticode is the inverse of all except the symbol bit (1 to 0 to 1) and the complement code is the anticode + 1.

For example,-1

Original code 10000000 00000000 00000000 00000001 anti-code 11111111 11111111 11111111 11111110 anti-complement code 11111111 11111111 11111111 11111111 Plus 1

As for how to write 32-bit data, I understand that integer data occupies 4 bytes and each byte has 8 bits, a total of 32 bits, one of which is a symbol.

Here is a brief introduction to binary.

Classification of PHP median operators

  • $ A & $ B And (bitwise And) sets the bits of $ a And $ B to 1.

  • $ A | $ B Or (bitwise Or) sets any one of $ a and $ B to 1.

  • $ A ^ $ B Xor (bitwise Xor) sets one of $ a and $ B as 1 and the other as 0 to 1.

  • ~ $ A Not (bitwise inversion) sets the bit 0 in $ a to 1, and vice versa.

  • $ A <$ B Shift left (left Shift) moves the bits in $ a to the left $ B (each movement represents "multiplied by 2 ").

  • $ A> $ B Shift right (right Shift): move the bits in $ a to the right $ B (each movement means "divided by 2 ").

Example:

And (1 1 = 1; 1 0 = 0; 0 0 = 0)

$ A = 1 & 2; // when both the "and" operations are 1, other 0 // 1 completion codes // 00000000 00000000 00000000 00000001 00000000 // 2 completion codes // 00000000 00000000 00000010 00000000 00000000 // result // 00000000 00000000 = 0 echo $; ----- result ----- 0

Or (1 1 = 1; 1 0 = 1; 0 0 = 0)

$ A = 1 | 2; // when at least one of the "or" operations is 1, other 0 // 1 completion codes // 00000000 00000000 00000000 00000001 00000000 // 2 completion codes // 00000000 00000000 00000010 00000000 00000000 // result // 00000000 00000011 = 3 echo $; ----- result ----- 3

XOR (1 0 = 1; 1 1 = 0; 0 0 = 1)

$ A = 3 ^ 2; // perform the "xor" operation. one is 1, and the other is 1 when the other is 0, other 0 // 1 completion codes // 00000000 00000000 00000000 00000011 00000000 // 2 completion codes // 00000000 00000000 00000010 00000000 00000000 // result // 00000000 00000001 = 1 echo $; ----- result ----- 1

Invert (1 = 0; 0 = 1) pay attention to the changes in the symbol bit.

$ A = ~ 2; // perform the "inverse" Operation 1 to 0, 0 to 1 // 2 binary // 00000000 00000000 00000000 00000010 11111111/11111111 11111111 11111101 first 1, is negative // reverse code // 11111111 11111111 11111111 11111100 10000000 // original code // 00000000 00000000 00000011 =-3 echo $ a; ----- result ------ 3

Shift left (move the bits in $ a to the left for $ B (each movement represents "multiplied by 2 "))

When the left shift is performed, the right side is filled with zero. when the symbol bit is removed, the plus and minus signs are not retained.

$ A = 1; // binary: // 00000000 00000000 00000000 00000001 // fill the right side with zero when moving left. the removed sign position means that the plus and minus signs are not retained. // 00000000 00000000 00000000 00000100 = 4 echo $ a <2; // Move the bits in $ a twice to the left. ----- result ----- 4

When the right shift is performed, the left side is filled with signs, which means the plus and minus signs are retained.

$ A = 4; // when the right shift is performed, the left side is filled with signs, meaning that the plus and minus signs are retained. // 00000000 00000000 00000000 00000100 00000000 // 00000000 00000000 00000001 = 1 echo $ a> 2; ----- result ----- 1

Here, the bitwise operator arithmetic is basically finished ......

Logical operators

The logical operator is the judgment logic. its return result is of the Boolean type, such as and or not. The logical operators in PHP include:

  • $ A and $ B And (logical and) TRUE, if both $ a And $ B are TRUE.

  • $ A or $ B Or (logical or) TRUE, if $ a Or $ B is TRUE.

  • ! $ A Not (logical Not) TRUE, if $ a is Not TRUE.

  • $ A & $ B And (logical And) TRUE, if both $ a And $ B are TRUE.

  • $ A | $ B Or (logical Or) TRUE. if $ a Or $ B is set to TRUE.

Logic and(When both sides of the expression are true, the result is true)

Two & symbols:

$ A = 2; $ B = 3; if ($ a> 1 & $ B> 2) {// true on the left, true on the right, true on both sides, the result is true; echo 'and';} ----- result ----- and

One & symbol:

$ A = 2; $ B = 3; if ($ a> 1 & $ B> 2) {// true on the left, true on the right, true on both sides, the result is true; echo 'and';} ----- result ----- and

Differences: Two & Hours: if a logical judgment expression is false, the subsequent logical operation will not be executed, which is commonly known as short circuit.

A &: Both sides are judged. Whether it is true or false.

Logic or(True when either of the two expressions is true)

Two | symbols:

$ A = 2; $ B = 4; if ($ a> 1 | $ B <3) {// true on the left and false on the right; echo 'or';} ----- result ----- or

One | symbol:

$ A = 2; $ B = 4; if ($ a> 1 | $ B <3) {// true on the left and false on the right; echo 'or';} ----- result ----- or

Differences: Two | Time: If a logic judgment expression is true, the subsequent logic operation will not be executed, short circuit.

One |: Both sides are judged. Whether it is true or false.

Non(! True = false ;! False = true)

$ A = true; var_dump (! $ A); ----- result ----- bool (false)
Ternary operators

Syntax: conditional expression? Expression 1: Expression 2.
Note: the position before the question mark is the condition for judgment. if the result is Boolean, expression 1 is called when the value is true, and expression 2 is called when the value is false.
The logic is: "If it is true, execute the first, otherwise execute the second ."

$ A = 456; $ B = $ a> 10? True: false; // the result before the question mark is true. execute the first true; var_dump ($ B); ----- result ----- bool (true );

The ternary operator can be said to be if... Else's simplified version will also be frequently used in future development.

Error Control Operator

Description: PHP supports an error control operator :@. When it is placed before a PHP expression, any error information that may be generated by this expression is ignored.

If you do not want the user to see the error message, you can use it.

Summary

After talking about this, we have finally finished most operators, including array operators and type operators, which will be said in arrays and objects.

The above is the basic PHP Tutorial 3 operator content. For more information, please follow the PHP Chinese network (www.php1.cn )!

Contact Us

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.