Three types of operators in PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:16:51
Original
904 people have browsed it

Three types of operators in PHP

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

The first one is a unary operator, which only operates on one value, such as ! (inversion operator) or ++ (plus one operator).

Example

1. Usage of ++i (take a=++i, i=2 as an example)

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

  Then the final a value is equal to 3 and the i value is equal to 3.

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

 2. Usage of i++ (take a=i++, i=2 as an example)

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

  Then the final a value is equal to 2 and the i value is equal to 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. When ++i and i++ are used alone, they are equivalent to i=i+1

If assigned to a new variable, ++i first adds 1 to the i value, and i++ first assigns i to the new variable

The second type is a binary operator that accepts two values, such as the familiar arithmetic operators + (addition) and - (subtraction), which are most PHP operators

 $a =1+2;

 $b =3-1;

The third type is the ternary operator, which accepts three values. It should be used to select one of two expressions based on one expression, rather than selecting between two statements or program routes. . (It may also be called a conditional operator, which may be more appropriate)

The code format is as follows: (expr1) ? (expr2) : (expr3);

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/895547.htmlTechArticlePHP’s three types of operators Operators are one or more values ​​that can be given (using programming In jargon, expression) to produce another value (thus the entire structure becomes an expression)...
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