Home  >  Article  >  Backend Development  >  A Deep Dive into Object-Oriented Programming in WordPress: Control Structures II

A Deep Dive into Object-Oriented Programming in WordPress: Control Structures II

王林
王林Original
2023-09-02 12:13:06525browse

A Deep Dive into Object-Oriented Programming in WordPress: Control Structures II

If you have been following our entire series, then you no doubt know that we are looking at the concepts of object-oriented programming from a beginner's perspective.

Specifically, we are looking at this topic for those who want to familiarize themselves with the paradigm and how to apply these concepts in the context of WordPress; however, before we can start working with object-oriented programming and WordPress, we must use the basics provided by PHP Functionality lays the foundation.

Ultimately, we work on creating a practical example of object-oriented programming by creating a WordPress plugin. But before that, there are a few things we need to discuss.

If this is your first time reading this series, then I recommend checking out the previous articles, as each article in this series builds on the previous one. If, on the other hand, you are more familiar with object-oriented programming, you may want to revisit this series when we start applying these ideas in practice.

So far we have discussed the following topics.

  1. Introduction
  2. course
  3. type
  4. Control structure: conditional statement

In this article, we will conclude our discussion of control structures by looking at the various types of loops provided by PHP.

Control Structure Review

If you read the previous article, then you will remember that "control structures" refer to structures provided by the language (in our case PHP) that allow us to modify the code throughout the program based on numbers. condition.

In the previous article, we looked at how to do this by using conditional statements, which will force the program to depend on a condition (for example, if a variable is set, if the condition is true, and so on).

cycle

But that's not the only type of control we have. In addition to conditions, we are also able to iterate (or loop) over the dataset so that we can take actions on each item in the dataset. We can add data, delete data, display data, sort data, and more.

For example, let's say we have a set of data, maybe 10 posts, and we want to loop and print out the title and date of each post. Loops allow us to do this.

No matter what you want to do, PHP provides four types of loops that allow us to iterate through the data set. In this article, we'll look at each example and some of the nuances of each so that you can use another set of control structures when you start writing object-oriented code.

for Loop

Due to the nature of how code is written, for loops are often considered the most complex of the loops. The flow of it reads a little unnaturally.

Normally, we are used to writing code line by line so that each instruction is set on a line; however, for for loops, we have a slightly different approach. First, I'll share a basic example and we'll cover various aspects of it, and then we'll look at a more detailed approach.

Here is a basic example that will count to 10 and display each number on the screen:

for ( $i = 0; $i < 10; $i++ ) {
    echo $i;
}

In the first line of the loop (inside the brackets after the for statement), we do the following:

  • Initialize variables $i and set them to zero
  • Set a condition to continue running when $i
  • Increment $i by the value 1 after each iteration (using the post-increment operator)

In the loop body, we just use PHP's echo function to print the current value of $i. As the loop processes the instructions, we will see 0 - 9 printed on the screen (because we are running from zero and $i is less than 10).

Different people have different techniques for reading code, so the strategy I'm about to recommend may not be the best for you, but when I read these types of loops, I usually read like this:

$i starts from zero, when $i is less than 10, the loop body is executed, and then $i is incremented by 1.

The problem is that $i can start at any value, can count to any number, and can increment by any value.

The more work we do with the for loop, the more likely you are to find something that can optimize performance. However, for now, the basics of for loops have been covered, and the advanced techniques are a bit beyond the scope of this article.

foreach 循环

foreach 循环与 for 循环类似,因为它们迭代数据集,但它们是按顺序执行的。这意味着没有简单的方法可以迭代列表中的每一项(例如两个项目)(例如,可以使用 $i + 2 in for 循环)。

这种类型的循环可以说是最具可读性的。对于我们的示例,假设我们有一个数组,并且数据数组包含以下名称:Alpha、Bravo、Charlie、Delta、Echo 和 Foxtrot。数组的名称存储在名为 $names 的变量中。

在这种情况下,我们可以设置一个 foreach 循环来迭代名称并将每个名称显示在屏幕上,如下所示:

$names = array( 'Alpha', 'Bravo', 'Charlie', 'Delta', 'Echo', 'Foxtrot' );
foreach ( $names as $name ) {
    echo $name;
}

设置非常简单,不是吗?

正如我们在上一节中分享了一种读取初始 for 循环的方法一样,您可以通过以下方式读取 foreach 循环:

对于名称集合中的每个名称,将其显示在屏幕上。

或者,也许更一般地说:

对于集合中的每个元素,将其显示在屏幕上。

还有什么吗?

foreach 循环还有另一个方面,我们可能会在本系列后面更详细地介绍,但主要思想是,如果您熟悉关联数组,即数组使用键进行索引并且具有关联值,您可以设置 foreach 循环来检索循环每次迭代的每个键和值。

例如,假设我们有以下关联数组:

$heroes = array( 'alien' => 'Superman', 'human' => 'Batman' );

当使用这样的关联数组时,您还可以设置一个 foreach 循环,如下所示:

foreach ( $heroes as $type => $name ) {
    echo $name . ' is a ' . $type;
}

这将导致输出读取诸如“超人是外星人”之类的内容,因为“超人”是值,“外星人”是他的类型(或他的键)。

此循环的更通用形式如下:

foreach ( $collection as $key => value ) {
    // Work goes here
}

没什么特别复杂的。

while 循环

介绍了 for 循环的变体后,是时候将我们的注意力转向 while 循环了,其中有两种变体(尽管它们的名称不同:while 循环和 do 循环),但它们只有很小的区别。

但是在我们了解它们有何不同之前,我们先来看看 while 循环,它的基本格式、如何使用它以及它与之前的 for 循环。

首先,while循环的基本格式如下:

while ( condition ) {
    // do work
}

请注意,此循环与之前的循环不同,它接受条件作为初始语句的一部分(这就是我们在上一篇文章中介绍条件的原因)。

循环的工作原理是首先检查条件是否为真,执行循环体中的代码块,然后再次检查条件。这意味着 while 循环可以执行任何工作单元,只要指定的条件计算结果为 true。

所以,当然,您可以迭代数字列表,运行数据集合,但您也可以在布尔值仍然为 true 的情况下执行某些操作。一旦标志达到 false,则 while 循环将终止。

让我们看一个从数组中弹出元素的示例。假设初始数组有 100 个元素,我们将一直这样做,直到数组剩下 50 个元素:

while ( 50 !== count ( $elements ) ) {
    array_pop( $elements );
}

这里,条件将继续评估为 true,直到 $elements 数组中的项目数减少到 50 项。

正如我们对之前的循环所做的那样,这是读取 while 循环的一种方法:

当这个条件成立时,执行以下代码。

当然,代码看起来就是这样,不是吗?

do 循环

最后,do 循环几乎完全类似于 while 循环,只不过在检查条件之前至少会迭代一次。

在看示例之前,先介绍一下 do 循环的基本结构:

do {
    // do work
} while ( condition );

比较清楚,对吧?

因此,让我们设置一个非常基本的示例,其中创建一个数组并仅用偶数填充它。为此,我们需要:

  • 保存数字的数组
  • 一个变量,用于保存我们迭代前 100 个偶数的次数

话虽如此,我们可以将代码设置为如下所示:

$i = 1;
$even_numbers = array();

do {

    if ( 0 === ( $i % 2 ) ) {
        $even_numbers[] = $i;
    }
    
    $i++;

} while ( $i <= 100 );

最后,与其余循环一样,您可以通过以下方式读取这些类型的循环:

Execute the following work block and then check whether the following conditions are true. If so, get on with the job.

Anything else?

When it comes to do loops, if you are using a collection, you need to make sure that the collection is not empty before processing it as it will be checked to see if the condition is true Before executing the code in the loop block. This may cause errors if you try to process an empty dataset.

Of course, if you are planning to do this, one of the other loops may be better optimized for this kind of operation.

Only use a do loop to indicate the number of executions that should be performed if you have a set of data or you are performing a procedure that you know needs to be performed at least once before checking condition.

What’s next?

With these, we've laid out everything you need to do to start discussing functions, properties, scopes, and other fundamental aspects of object-oriented programming.

For those of you who think this series has more or less covered some of the basics of PHP programming, we'll start to get into slightly more advanced territory in the next article.

The above is the detailed content of A Deep Dive into Object-Oriented Programming in WordPress: Control Structures II. For more information, please follow other related articles on the PHP Chinese website!

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