Tutorial on the basics of operators in php

伊谢尔伦
Release: 2023-03-11 07:36:01
Original
1482 people have browsed it

Operator is a symbol used to perform certain operations on arrays and variables. Control structures If we want to effectively respond to user input, the code needs to have the ability to judge. The structure that allows the program to make judgments is called a condition

Operator

Operator is used to perform certain operations on arrays and variables symbol.

1. Arithmetic operators

##-minus$ a-$b#* Operator

Operators

Name

Example

##+

Add

##$a+$b

##multiply

$a*$b

##/

except

##$a/$b

%

##remainder

$a %$b

##2. Compound assignment operator

Usage method is equivalent to ##$a+=$b##-=$a-=$b$a=$a%$b$a=$a.$b

+=

$a=$a+$b

##$a=$a-$b

*=

$a*=$b

$a=$a*$b

##/=

$a/=$b

$a=$a/$b

##%=

##$a%=$b

##.=

$a.=$b

Prefix increment and decrement and postfix Increment and decrement operator :

$a=++$b;

$a=$b++;

$a=--$b;

$a=$b--;

3, Comparison operator

##Not equal$a!=$b##!= =<>##$a>$bless than or equal to#>=Greater than or equal

Operator

Name

How to use

= =

equals

$a= =$b

##= = =

hengheng

##$a= = =$b

!=

Not Identity

##$a!= =$b

Not equal

## $a<>$b

#<

Less than

$a<$b

#>

is greater than

##<=

$a<=$b

##$a>=$b

##

Note: Identity means that only if the operands on both sides are equal and the data types are also equivalent, true will be returned;

For example: 0= = "0" This returns is true because the operands are equal

0== ="0" This returns false because the data types are different

4, Logical operators

##&& and ##if priority $a or $b is the same as

Operator

Usage

How to use

Instructions

!

##non

##!$b

If

$b is false, returns true;Otherwise the opposite

## with

##$a&&$b

##if$a

$b are both true, then the result is true; otherwise false##||

or

##$a||$b

Either one of $a

and

$b
is

true or both are true## When #, the result is true; otherwise, it is false##and

##与

$a and $b

## is the same as &&

, but its
is lower

##or##or

||

, but its priority is higher Low

Operator"and""or" is better than &&and || has a lower priority.

5. Ternary operator

Condition ? value if true : value if false

Example: ($grade>=50 ? "Passed " : "Failed")

6. Error suppression operator:

$a=@(57/0);

The divisor cannot be 0 will cause an error, so add @ to avoid error warnings.

7. Array Operator

##United!$bReturns a message containing ##= = and $bNon-equivalent$a are not equivalent, return $btrue

Priority and associativity of operators:

Generally speaking, operators have a set of priorities, which is the order in which they are executed.

Operators are also associative, that is, the execution order of operators with the same priority. This order is usually left to right, right to left or irrelevant.

The table of operator precedence is given below. The top operator has the lowest priority, and the priority increases from top to bottom in the table.

Operator precedence

##Operator

How to use

How to use

Instructions

##+

$a

and $ Array of all elements in b

Equivalent

##$a&&$b

##If $a

$b have the same elements, return true

= = =

##hengheng

$a||$b

##If

$a and

With the same elements and the same order, return true##!=

##$a and $b

If

and $b

true#<>

##Not equivalent

If

$a and

is not equivalent, returns

!= =

##non-identity

$a or $b

if $a and $b is not identical, returns true

##Print##leftLeftLeft#<<= >>=##<< >>+ - .* / %! ~ ++ -- (int)(double)(string)(array)(object) @[]

Associativity

Operator

##left

,

left

##Or

left

Xor

left

And

right

##= += -= *= /= .= %= &= |= ^= ~= <<= >>=

?

:

||

##left

&&

left

##|

left

^

Left

&

Not relevant

##= = != = = = = != =

Irrelevant

left

left

Left

right

right

Not relevant

##New

irrelevant

()

In order to avoid priority confusion, you can use parentheses to avoid priority.

The above is the detailed content of Tutorial on the basics of operators in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!