Table of Contents
" > (1) Definition
(七)逻辑运算符
(八)运算符的优先级
(九)其他运算符
流程控制
(一)顺序执行" >(一)顺序执行
(二)分支执行" >(二)分支执行
 补:switch语句(分支执行)
(三)循环执行" >(三)循环执行
1.while语句" >1.while语句
2.do...while循环" >2.do...while循环
3.for语句" >3.for语句
附加练习:用php输出乘法口诀" >附加练习:用php输出乘法口诀
4.break语句" >4.break语句
5.continute语句" >5.continute语句
6.exit()语句" >6.exit()语句
Home Backend Development PHP Problem PHP basic consolidation operators and process control

PHP basic consolidation operators and process control

Apr 12, 2022 am 11:42 AM
php

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
	
?>
Copy after login

(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;
?>
Copy after login

(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)
    ?>
    Copy after login

(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)
?>
Copy after login

        注: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)
?>
Copy after login

(八)运算符的优先级

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

(九)其他运算符

<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;//屏蔽没有定义的错误
?>
Copy after login

流程控制

(一)顺序执行

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

<?php
	echo "first";echo "<hr>";
	echo "second";echo "<hr>";
	echo "third";echo "<hr>";
?>
Copy after login

(二)分支执行

  • 单向条件
  • 双向条件
  • 多向条件
<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 "不及格";
	}
Copy after login

 补: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天的!";
	}
?>
Copy after login

 用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;
		}
	}

?>
Copy after login

 

2.do...while循环

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

3.for语句

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

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

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

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>";
	}
?>
Copy after login

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>";
	}
?>
Copy after login

6.exit()语句

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

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

推荐学习:《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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles