PHP's three kinds of operators

Source: Internet
Author: User
Tags arithmetic operators

An operator is something that can produce another value (thus the entire structure becomes an expression) by giving one or more of the values (in programming jargon, an expression).

The first is a unary operator, which only operates a single value, for example! (Take the inverse operator) or + + (plus an operator).


1, the use of ++i (A=++i, i=2 as an example)

First add the I value by 1 (that is, i=i+1), and then assign the variable a (that is, a=i),

The final a value is equal to 3, and the I value equals 3.

So a=++i is equivalent to i=i+1, a=i

2, the use of i++ (a=i++, i=2 as an example)

First assigns the I value to the variable a (that is, a=i), and then the I value plus 1 (that is, i=i+1),

The final a value is equal to 2, and the I value equals 3.

So a=i++ is equivalent to A=i, i=i+1

3, ++i and i++

A=++i is equivalent to i++, a=i

a=i++ is equivalent to A=i, i++

4, ++i and i++ when used alone, the equivalent of i=i+1

If you assign a new variable, ++i first adds the I value to 1, and i++ first assigns I to the new variable

The second is the two-dollar operator, which accepts two values, such as the familiar arithmetic Operators + (plus) and-(minus), and most PHP operators are these

$a =1+2;

$b = 3-1;

The third is the ternary operator, which accepts three values and should be used to select one of the other two expressions based on an expression, rather than to select it in two statements or program routes. (also referred to as a conditional operator may be more appropriate)

The code format is as follows: (EXPR1)? (EXPR2): (EXPR3);

For example: $page =!empty ($_get[' page '])? $_get[' Page ']: 1;

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.