


PHP Reading Notes (5) - Structural Statements, PHP Reading Notes_PHP Tutorial
PHP reading notes (5) - structural statements, php reading notes
PHP structural statements
Sequential structure
The sequence structure is like a straight line, which is executed in order. The code we write is executed in a sequential structure by default.
Conditional structure if…else…
The conditional structure is like a fork in the road, you can go left or right. For example, when we go to the bathroom, we know our gender. At this time, we need to follow the conditions provided by the bathroom, the men's bathroom on the left, the women's bathroom on the right, or just the opposite, where gender is the condition of this conditional structure. For another example, it is now popular to use A, B, and C to grade scores. Assume that the test score is 93 points, which can be set to level A. If the test score is 87, it can be set to level B. The score range here is Conditions in conditional structures.
The "if...else..." syntax in PHP is as follows:
<span>1</span> <?<span>php </span><span>2</span> <span>if</span><span>(条件){ </span><span>3</span> <span>//</span><span>分配服务器干的任务A</span> <span>4</span> }<span>else</span><span>{ </span><span>5</span> <span>//</span><span>分配服务器干的任务B</span> <span>6</span> <span>} </span><span>7</span> ?>
Through conditional judgment, if the return value is Boolean TRUE, task A will be executed. If the return value is FALSE, task B will be executed.
Conditional structure if…else if…
The "if...else if..." syntax in PHP is as follows:
<?<span>php </span><span>if</span><span>(条件一){ </span><span>//</span><span>分配服务器干的任务A</span> }<span>else</span> <span>if</span><span>(条件二){ </span><span>//</span><span>分配服务器干的任务B</span> <span>} </span>?>
Judgment by condition one , if the return value is a Boolean value TRUE, then execute task A, if the return value is FALSE, then judge condition two, if the return value is a Boolean value TRUE, Then execute task B, otherwise neither task A nor task B will be executed. The server will continue to perform other tasks.
Conditional structure switch…case…
The "switch...case..." syntax in PHP is as follows:
<span> 1</span> <?<span>php </span><span> 2</span> <span>switch</span><span> (条件) </span><span> 3</span> <span>{ </span><span> 4</span> <span>case</span> 条件值一: <span> 5</span> <span>//</span><span>任务一</span> <span> 6</span> <span>break</span><span>; </span><span> 7</span> <span>case</span> 条件值二: <span> 8</span> <span>//</span><span>任务二</span> <span> 9</span> <span>break</span><span>; </span><span>10</span> <span>default</span>: <span>11</span> <span>//</span><span>默认任务</span> <span>12</span> <span>} </span><span>13</span> ?>
First determine the condition. If the return value of the condition is condition value one, then execute task one. If the return value of the condition is condition value two, then execute task two. If the return value of the condition is neither condition value one, If the condition value is not two, the default task will be executed. The function of break is to end the switch. Using the switch statement can avoid lengthy "if..else if..else" code blocks.
The function of break is to prevent the code from continuing to execute in the next case.
The while loop statement of loop structure in PHP
The loop structure is like running around the football field in circles, finishing one circle and then another. In other words, a certain task is repeatedly performed under certain conditions. Like a 400-meter track, if you run 800 meters, you run 2 laps. When you finish the first lap, you run the second lap. At the end of the second lap, you have reached 800 meters, and you stop running.
In PHP, the while loop statement is as follows:
<?<span>php </span><span>while</span><span>(条件){ </span><span>//</span><span>执行任务</span> <span>} </span>?>
First determine whether a certain condition is met (whether the condition return value is TRUE), if it is met, execute the task, complete the task, and then determine whether the condition meets the requirements. If it is met, repeat the task, otherwise end the task.
The do while loop statement of loop structure in PHP
There is another type of loop statement in PHP: do...while loop statement syntax is as follows:
<span>1</span> <?<span>php </span><span>2</span> <span>do</span><span>{ </span><span>3</span> <span>//</span><span>执行任务</span> <span>4</span> }<span>while</span><span>(条件) </span><span>5</span> ?>
First execute the task (the while statement in the previous section first determines whether the condition is true, and then executes the task). After executing the task, determine whether a certain condition is met (whether the condition return value is TRUE), and if it is met, then Execute the task again, complete the task, and continue to determine the conditions.
The difference between while and do...while statements in loop structures in PHP
The difference between while and do...while loop statements is that while first determines whether the condition is true, and then executes the loop, do...while first executes the task once, and then determines whether to continue executing the loop, that is, do. ..while will execute the task at least once. When the condition is FALSE, the task in while will not be executed once, and the task in do...while will be executed once.
The for loop statement of loop structure in PHP
There is also a loop statement in PHP. The structure of the for loop statement is as follows:
<span>1</span> <?<span>php </span><span>2</span> <span>for</span><span>(初始化;循环条件;递增项){ </span><span>3</span> <span>//</span><span>执行任务</span> <span>4</span> <span>} </span><span>5</span> ?>
In the for statement, "initialization" is evaluated unconditionally once before the loop starts, and "loop condition" is evaluated before each loop starts. If the value is TRUE, the loop continues and the loop body statement (execution task) is executed. If the value is FALSE, the loop is terminated. The "increment term" is evaluated (executed) after each loop. It is often used to loop through a block of code a specified number of times.
PHP中循环结构之foreach循环语句
在PHP中foreach循环语句,常用于遍历数组,一般有两种使用方式:不取下标、取下标。
(1)只取值,不取下标
<span>1</span> <?<span>php </span><span>2</span> <span>foreach</span> (数组 <span>as</span><span> 值){ </span><span>3</span> <span>//</span><span>执行的任务</span> <span>4</span> <span>} </span><span>5</span> ?>
(2)同时取下标和值
<span>1</span> <?<span>php </span><span>2</span> <span>foreach</span> (数组 <span>as</span> 下标 =><span> 值){ </span><span>3</span> <span>//</span><span>执行的任务</span> <span>4</span> <span>} </span><span>5</span> ?>
上一节:PHP读书笔记(4)-运算符

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.