Home  >  Article  >  Backend Development  >  PHP basic consolidation operators and process control

PHP basic consolidation operators and process control

WBOY
WBOYforward
2022-04-12 11:42:161153browse

This article brings you relevant knowledge about PHP, which mainly introduces issues related to operators and process control, including the classification and definition of operators and some common operators Usage, as well as sequence execution, branch execution, loop execution and other related content of process control. I hope it will be helpful to everyone.

PHP basic consolidation operators and process control

## Recommended learning: "

PHP Video Tutorial"

Operator

(1) Definition

                                         

Operator is a symbol that performs a certain operation on one or more operands (variables or values) , so it is also called an operator.

(2) Classification

  1. ## Assignment operator
  2. Arithmetic operators
  3. ## Comparison operators
  4. Logical operators
  5. Concatenation operators
  6. Error suppression Symbol
  7. Ternary operator
  8. Self-operation operator
  9. Bitwise operators
  10. ##(3) Arithmetic operators

<?php
	$a = 5;
	$b = 6;
	
	echo -$a;echo "<br>";  //-5
	echo $a - $b;echo "<br>";	//-1
	echo $a * $b;echo "<br>"; //30
	echo $a / $b;echo "<br>";//6/5
	echo $a%$b;echo "<br>"; //5
	echo $a++;echo "<br>";//6,此时$a的值为6
	echo $a;echo "<br>";//6
	echo ++$a;echo "<br>";//7
	echo $a--;echo "<br>";//7,此时$a的值为6
	echo $a;echo "<br>";//6
	echo --$a;echo "<br>";//5
	
?>

(4) String operator (connection operator)

         In PHP, use

.
for string splicing, also called the connection operator;

and in JS, use for string splicing, visit The attributes in the object use .

<?php
	$name = "xiaofeng";
	$str = "hello,".$name;echo "<br>";//.讲$name和hello,拼接起来
	//$str = "Hello," + $name;echo "<br>";//0
	echo $str;
?>

(5) Assignment operator

=: The right side is assigned to the left side
  • .=: The left side is connected to the right side String, and then assign it to the left
  • =: add the result on the left to the right, and then assign it to the left
  • -=: Subtract the result of the right from the left, and then assign it to the left
  • *=: Multiply the result of the left by the right , and then assign the value to the left
  • /=: the result of the left cooking skill on the right, and then assign the value to the left
  • %=: Take the result on the left modulo the result on the right, and then assign it to the left
  • <?php
    	$a = 6;$b =5;
    	$b +=$a;echo $b;echo '<br>';//11,此时$b=11
    	$b -=$a;echo $b;echo '<br>';//11-6=5,此时$b=5
    	$b *=$a;echo $b;echo '<br>';//5*6=30,此时$b=30
    	$b /=$a;echo $b;echo '<br>';//30/6=5,此时$b=5
    	$b %=$a;echo $b;echo '<br>';//5
    	$b .=$a;echo $b;echo '<br>';//56 .相当于字符串连接符讲5和6拼接起来了,属于字符串
    	
    	var_dump($b)
    ?>

(6) Comparison operators

<?php
	$a = 6;$b = 4;
	$c = $a == $b;//false
	$c = $a === $b; //false
	$c = $a != $b;//true
	$c = $a !== $b;//true
	$c = $a <> $b;//true
	$c = $a > $b;//true
	$c = $a >= $b;//true
	$c = $a <= $b;//true
	var_dump($c)
?>

        注:PHP规定使用echo输出布尔类型的时候,echo true输出为1;echo false页面什么都不输出。

(七)逻辑运算符

  • &&:逻辑与,两个表达式参与运算,都为真则返回真,否则返回FALSE
  • ||:逻辑或,两个表达式参与运算,一个为真就返回真,都为假返回FALSE
  • !:逻辑非,一个表达式参与运算,为真则返回FALSE,为假则返回TRUE

<?php
	$a = true;
	$b = false;
	$c = $a and $b;//true,赋值运算的优先级比and高
	$c = ($a and $b);//flase
	$c = ($a && $b);//false 
	$c = ($a or $b);//true
	$c = ($a || $b);//true
	$c = ($a xor $b);//true
	$c = !$a;//flase 
	var_dump($c)
?>

(八)运算符的优先级

        谁的优先级高就先算谁的,并且规定了从哪个方向开始算的问题。

(九)其他运算符

<meta charset="gbk">
<h1>其他运算符</h1>
<?php
	#?问号——三元运算符
	$a = 10;
	$b = 20;
	$c = $a>$b?$a:$b;//20 意思是如果a>b,输出a反之输出b
	echo $c;echo "<hr/>";
	#~反引号——执行cmd命令
	$cmd="whoami";
	echo "<pre class="brush:php;toolbar:false">".`$cmd`;//执行whoami命令
	$d = "net user";
	echo `$d`;echo "<hr/>";
	#@符号——屏蔽运算错误
	$name;
	echo @$name;//屏蔽没有定义的错误
?>

流程控制

(一)顺序执行

        自上而下的执行即可,PHP语句默认执行的过程就是顺序执行这点跟PHP一样。

<?php
	echo "first";echo "<hr>";
	echo "second";echo "<hr>";
	echo "third";echo "<hr>";
?>

(二)分支执行

  • 单向条件
  • 双向条件
  • 多向条件
<meta charset = "gbk">
<h3>if——单向分支执行</h3>
<?php
	$pass = 60;
	$score = 60;
	if($score >= $pass){
		echo "恭喜你通过了!";
	}//如果通过了就输出echo,没通过则无回显。		
?>
<h3>if——双方向分支执行</h3>
<?php
	$pass = 60;
	$score = 59;
	if($score >= $pass){
		echo "Pass";
	}else{
		echo "挂了,sorry!";
	}
?>
<h3>if——多方向分支执行</h3>
<?php
	$pass = 60;
	$score = 59;
	if($score >= 85 && $score<=100){
		echo "优秀";
	}elseif($score >=75){
		echo "良好";
	}elseif($score >=60){
		echo  "及格";
	}else{
		echo "不及格";
	}

 补:switch语句(分支执行)

<meta charset = "gbk">
<h3>switch——分支执行</h3>
<?php
	$day = 29;
	switch($day){
		case 30:
			echo "小月";
			break;
		case 31:
			echo "大月";
			break;
		case 28:
			echo "平月";
			break;
		case 29:
			echo "没有一个月是29天的!";
	}
?>

 用switch语句注意一下几点:

  • case后面的语句是不需要()的
  • 每个case后面都不要忘记后面跟上break语句跳出循环
  • 如果case后面没有接上break,说明内容是同下的

(三)循环执行

  • while语句
  • do...while语句

1.while语句

<?php
	$i = 0;//计数器
	while($i < 4){ //循环条件
		echo $i++."<hr>";//.是将来分割线连接起来,相当于Python里面的end=""
	}

?>
<hr>
<?php
	$i = 1; //int(0)是flase,flase是不会循环
	while($i){
		echo $i++."<br>";
		if($i == 2){
			break;
		}
	}

?>

 

2.do...while循环

<?php
	$i = 0;
	do {
		echo $i++."<br>";
	}while($i < 5)
?>

3.for语句

<?php
	for($i = 0;$i < 6;$i++){
		echo "for循环遍历0到5,开始:"."$i"."<hr>";
	}
?>

附加练习:用php输出乘法口诀

<?php
	for($i = 1;$i < 10;$i++){
		for($j = 1;$j < $i+1;$j++){
			echo $i.'x'.$j.'='.$i*$j."    ";
		}
		echo "<br>";
	}
?>

4.break语句

        用于for、while、do...while、foreach、switch中断这些语句!后面用数字表示跳出几层循环,默认没有数字就表示跳出当前循环。

<meta charset = "gbk">
<h3>break语句</h3>
<?php
	for($i = 0;$i < 6;$i++){
		echo "for循环遍历0到5,开始:".$i."<br/>";
		for($j=1;$j<=5;$j++){
			echo $j;
			if($j == 2){
				#break;//只跳出本层循环
				break 2;//跳出两层循环
			}
		}
		echo "<br>";
	}
?>

5.continute语句

        用在循环语句中,代表着本次循环轮空,不是结束整个循环语句。

<meta charset = "gbk">
<h3>continue语句</h3>
<?php
	for($i = 0;$i < 6;$i++){
		if($i == 3){
			continue;//当i=3的时候,结束!
		}
		echo "for循环遍历0到5,开始:"."$i"."<hr>";
	}
?>

6.exit()语句

        用处是结束当前整个php脚本,awd的时候经常用到包括die()语句也是!

<?php
	for($i = 0;$i < 6;$i++){
		if($i == 3){
			exit("整个脚本到此执行完毕了哦!");//当i=3的时候,整个脚本结束!
		}
		echo "for循环遍历0到5,开始:"."$i"."<hr>";
	}
?>

推荐学习:《PHP视频教程

The above is the detailed content of PHP basic consolidation operators and process control. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete