Detailed examples of PHP operators and expression usages

Source: Internet
Author: User
Tags arithmetic operators bitwise operators integer division
Operators and expressions are the most basic knowledge of PHP learning, this article through an example to explain the operators and expressions in PHP some of the application and skills, as well as the need to pay attention to the place, the need for friends can refer to.

PHP Operators and expressions

I. Classification of Operators

1, by Operation number classification

1.!true//Unary operators
2. $a + $b//two-dollar operator
3.true? 1:0//ternary operator

2, according to the function classification

(1) Arithmetic operators
1.+ 、-、 x,/,% (take more)

(2) String operators
1.///For example: $a = ' abc '. ' EFG ';

(3) Assignment operator

1.=//Simple Assignment
2.+=,-=, x=,/=,%=,. =//Compound assignment
3.++ ($a + +, + + $a),--($a--、--$a)//increment decrement
4.& ($a = 1; $b = &a)//reference assignment

(4) Comparison operators

1.==, = = = (constant equals),! =,!===, <> (not equal to), <, >, <=, >=

(5) Logical operators
1.//in parentheses with higher precedence than outside parentheses
2.&& (and), | | (OR),! (not), XOR (heterogeneous),

(6) Bitwise operator
1.& (Bitwise AND), | (bitwise OR), ~ (bitwise non), ^ (bitwise XOR), << (left shift), >> (right Shift)

Second, arithmetic operators

% remainder, common usage: 1) integer Division operation 2) control range of values

Example: Judging whether it is a leap year (four years a leap, a century does not leap, 400 years again leap)


% will turn the number of the two sides into integer after the division//% can not be used with decimals or negative if ((($year%4 = = 0) && ($year%100! = 0)) | | $year%400 = 0) echo "Leap year"; Elseecho "common year" ;

Three, assignment operators


Add 10 First, then assign to yourself, equivalent to $a = $a +10$a + = 10; Increment decrement, increment decrement first + + $a//first assignment, then increment decrement $a++//instance $ A = ten; $b = $a + + $c =--$b result: a=11 b=9 c=9

Four, logical operators

XOR: Same as False (two true or two false = False), different for true (one true false = False)
Tips: Note the difference from or two true = True
Logical operator Short Circuit
1, &&//A false, then do not operate, must be false
2, | | One is true, then there is no operation behind it, it must be true.

fopen ("test.php", "R") or Die ("failure");

Tips: For Operators & | No short circuit characteristics

Five, bitwise operators

Bit operation: Convert integer to 32-bit binary, string to Ansca code to handle

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.