Home >Backend Development >PHP Tutorial >PHP operators and control structures_PHP tutorial

PHP operators and control structures_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:20:33740browse

操作符

操作符是用来对数组和变量进行某种操作运算的符号。

1、算术操作符

操作符

名称

示例

+

$a+$b

-

$a-$b

*

$a*$b

/

$a/$b

%

取余

$a%$b

2、复合赋值操作符

操作符

使用方法

等价于

+=

$a+=$b

$a=$a+$b

-=

$a-=$b

$a=$a-$b

*=

$a*=$b

$a=$a*$b

/=

$a/=$b

$a=$a/$b

%=

$a%=$b

$a=$a%$b

.=

$a.=$b

$a=$a.$b

前置递增递减和后置递增递减运算符:

$a=++$b;

$a=$b++;

$a=--$b;

$a=$b--;

3、比较运算符

操作符

名称

使用方法

= =

等于

$a= =$b

= = =

恒等

$a= = =$b

!=

不等

$a!=$b

!= =

不恒等

$a!= =$b

<>

不等

$a<>$b

<

小于

$a<$b

>

大于

$a>$b

<=

小于等于

$a<=$b

>=

大于等于

$a>=$b

注:恒等表示只有两边操作数相等并且数据类型也相当才返回true;

例如:0= ="0" 这个返回为true ,因为操作数相等

      0= = ="0"  这个返回为false,因为数据类型不同

4、逻辑运算符

操作符

使用方法

使用方法

说明

!

!$b

如果$bfalse,则返回true;否则相反

&&

$a&&$b

如果$a$b都是true,则结果为true;否则为false

||

$a||$b

如果$a$b中有一个为true或者都为true时,其结果为true;否则为false

and

$a and $b

&&相同,但其优先级较低

or

$a or $b

||相同,但其优先级较低

操作符"and""or"&&||的优先级要低。

5、三元操作符

Condition ? value if true : value if false

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

6、错误抑制操作符:

$a=@(57/0);

除数不能为0,会出错,所以加上@避免出现错误警告。

7、数组操作符

操作符

使用方法

使用方法

说明

+

联合

!$b

返回一个包含了$a$b中所有元素的数组

= =

等价

$a&&$b

如果$a$b具有相同的元素,返回true

= = =

恒等

$a||$b

如果$a$b具有相同的元素以及相同的顺序,返回true

!=

非等价

$a and $b

如果$a$b不是等价的,返回true

<>

非等价

 

如果$a$b不是等价的,返回true

!= =

非恒等

$a or $b

如果$a$b不是恒等的,返回true

操作符的优先级和结合性:

一般地说,操作符具有一组优先级,也就是执行他们的顺序。

操作符还具有结合性,也就是同一优先级的操作符的执行顺序。这种顺序通常有从左到右,从右到左或者不相关。

下面给出操作符优先级的表。最上面的操作符优先级最低,按着表的由上而下的顺序,优先级递增。

操作符优先级

结合性

操作符

Or

Xor

And

Print

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

:

||

&&

|

^

&

不相关

= =  != =  = = =  != =

不相关

<<= >>=

<< >>

+ - .

* / %

! ~ ++ -- (int)(double)(string)(array)(object) @

[]

不相关

New

不相关

()

To avoid priority confusion, you can use parentheses to avoid priorities.

Control structure

If we want to effectively respond to user input, the code needs to be judgmental. The structure that allows the program to make judgments is called a condition.

1. There are three structures of if..else loops
The first one only uses the if condition and treats it as a simple judgment. Interpreted as "what to do if something happens." The syntax is as follows:
if (expr) { statement }
where expr is the condition for judgment, usually using logical operation symbols as the condition for judgment. The statement is the execution part of the program that meets the conditions. If the program has only one line, the curly brackets {} can be omitted.
Example: This example omits the curly braces.

Copy code The code is as follows:

if ($state==1)echo "Haha " ;
?>

Special attention here is that the judgment of equality is == instead of =. ASP programmers may often make this mistake, = is an assignment.
Example: The execution part of this example has three lines, and the curly brackets cannot be omitted.
Copy code The code is as follows:

if ($state==1) {
echo "haha;
echo "
" ;
}
?>

The second is to add an else condition in addition to if , can be interpreted as "what to do if something happens, or how to solve it otherwise" The syntax is as follows:
if (expr) { statement1 } else { statement2 }
Example: Modify the above example to be more complete. Processing. Since there is only one line of instructions for execution, there is no need to add curly brackets
Copy the code The code is as follows:
<.>if ($state==1) {
echo "Haha" ;
echo "
";
}
else{
echo "Haha";
echo "
";
}
?>


The third type is the recursive if..else loop, usually It is used for various decision making. It combines several if..else and applies it directly:


Copy code The code is as follows:
if ( $a > $b ) {
echo "a is bigger than b" ;
} elseif ( $a = = $b ) {
echo "a is equal to b" ;
} else {
echo "a is smaller than b" ;
}
?>


The above example only uses a two-level if..else loop to compare the two variables a and b. When actually using this kind of recursive if..else loop, please use it carefully, because too many levels of loops can easily make the design Problems with the logic, or missing braces, etc., will cause inexplicable problems in the program.
2. There is only one type of for loop with no changes. Its syntax is as follows:
for ( expr1; expr2; expr3) { statement }
where expr1 is the initial value of the condition. expr2 is the condition for judgment. Logical operators are usually used as the condition for judgment. expr3 is the condition to be executed after executing the statement. The part is used to change the conditions for the next cycle to judge, such as adding one...etc. The statement is the execution part of the program that meets the conditions. If the program has only one line, the curly brackets {} can be omitted.
The following example is written using a for loop:


Copy the code The code is as follows:
for ( $i = 1 ; $i <= 10 ; $i ++) {
echo "This is the ".$i."th loop
" ;
}
?>


3. The switch loop usually handles compound conditional judgments. Each sub-condition is part of the case instruction. In practice, if many similar if instructions are used, they can be synthesized into a switch loop.
The syntax is as follows:
switch (expr) { case expr1: statement1; break; case expr2: statement2; break; default: statementN; break; }
The expr condition is usually a variable name. The exprN after case usually represents the variable value. After the colon is the part to be executed that meets the condition. Be sure to use break to break out of the loop.


Copy code The code is as follows:
switch ( date ( "D" )) {
case "Mon" :
echo "Today is Monday" ;
break;
case "Tue" :
echo "Today is Tuesday" ;
break;
case "Wed " :
echo "Today is Wednesday" ;
break;
case "Thu" :
echo "Today is Thursday" ;
break;
case "Fri" :
echo "Today is Friday" ;
break;
default:
echo "Today is a holiday" ;
break;
}
?>


What needs to be noted here is break; don’t omit it, default, omission is okay.
Obviously, using an if loop in the above example is very troublesome. Of course, when designing, the conditions with the greatest probability of occurrence should be placed at the front and the conditions with the least occurrence at the end, which can increase the execution efficiency of the program. In the above example, since the probability of occurrence is the same every day, there is no need to pay attention to the order of the conditions.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325064.htmlTechArticleOperator Operator is a symbol used to perform certain operations on arrays and variables. 1. Arithmetic operator operator name example + Add $a+$b - Subtract $a-$b * Multiply $a*$b / Divide $a/$b % Remainder...
Statement:
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
Previous article:php preg_filter performs a regular expression search and replace_PHP tutorialNext article:php preg_filter performs a regular expression search and replace_PHP tutorial

Related articles

See more