Home > Article > Backend Development > Object-Oriented Programming in WordPress: Control Structures I
For those of you who have been following this series so far, you know that we are looking at object-oriented programming specifically from a beginner's perspective.
This means that we are not only approaching the topic for those who are looking at how to get started using this paradigm, but we are looking at all the various features that make up the language and ultimately the implementation of PHP. Used in the context of object-oriented programming.
Additionally, we are doing all of this in the context of WordPress so that by the end of this series, we can see a practical application of how to use all of this in real-world examples. p>
If this is your first time reading an article in this series, then I highly recommend checking out the previous articles, as each article in this series builds on the previous one.
So far we have covered the following:
In this article, we will discuss control structures.
"Control structure" is a fancy term that describes how we, ahem, control how code flows through our program based on a variety of factors.
For example, let's say we want to execute a specific set of instructions, but you want to do something when one variable is set or another set of instructions sets another variable.
Or suppose you have a set of data and you want to loop through each value, set each specific value, or even create specific values.
Regardless of the situation, the way to do this is through the use of a control structure. In the remainder of this article, we will introduce two types of control structures: conditionals and loops.
Although conditionals and loops are the types of control structures we will discuss, there are subsets of each type.
For example, the conditional statement is:
if/then
Statementswitch/case
StatementOn the other hand, there are some other variations of loops:
for
foreach
do
while
While these may be new constructs to some, we've covered the basics in previous articles, so we have everything we need to move forward.
In my opinion, conditional statements are the easiest statements to understand because they read more like sentences than many other types of programming statements. For example, if you literally mean "If this condition is true, do this; otherwise, do this."
Of course, if you also need to check some other conditions before deciding to take action, things get slightly complicated, but the gist is still the same.
Having said that, let’s first look at one of the two conditional variants provided by PHP.
if/then
StatementAs mentioned earlier, the most basic form of a conditional statement is if/else
. You will usually see it written like this:
<?php if ( condition ) { // Take on action } else { // Take another action }
Of course, this still doesn't really explain how control structures work, does it? I mean, sure, it provides some framework for how to look at it, but it leaves a lot more to be desired.
In other words, what is this condition line? Second, what courses of action can the control structure take?
First, Condition refers to any statement that can be evaluated as a Boolean expression. Reasonable? In short, a condition represents any statement that can evaluate to true
or false
.
For example, let's say we have two values:
$is_active
$total_count
These are obviously some generic values, but suppose that if $is_active
is set to true, then we will increase $total_count
by one; otherwise, we will increase $total_count
minus one.
code show as below:
<?php $is_active = true; if ( $is_active ) { $total_count = $total_count + 1; } else { $total_count = $total_count - 1; }
In the example above, $total_count
will be incremented by one because $is_active
evaluates to true.
Alternatively, assume $is_active
is set to false
.
<?php $is_active = false; if ( $is_active ) { $total_count = $total_count + 1; } else { $total_count = $total_count - 1; }
In this example, $total_count
will be decremented by one because $is_active
evaluates to false.
Now, before we look at the next example, it's important to understand that these are extremely trivial examples. The purpose of these examples is not to show how to take complex operations and combine them into conditional constructs, but rather to show how to use conditional constructs.
When we get to the part of this series where we start writing plugins, you'll see how to use more complex expressions in real applications.
话虽如此,让我们再看一个 if/then 语句的示例。在此示例中,我们将查看 if/elseif/else
。首先,我们假设 $is_active
设置为 true,并且 $total_count
设置为 10。
<?php $is_active = false; $total_count = 10; if ( $is_active ) { $total_count = 1; } else if ( $total_count >= 10 ) { $total_count = $total_count + 1 } else { $total_count = $total_count - 1; }
上面的代码可以这样理解:
$is_active
设置为 true,则将 $total_count
设置为 1。 $is_active
不正确。$total_count
大于或等于 10,则将 $total_count
加 1。 $total_count
等于 10,因此我们将 $total_count
增加到 11。$total_count
不大于或等于 10,那么我们会将 $total_count
减 1。当上面示例中的代码块执行完毕时,$total_count
将等于 11。
有道理吗?
这就是我们称之为控制结构的原因:这些语句(或评估)使我们能够根据某些条件确定要运行哪些代码。
对于那些已经编程了一段时间的人来说,您应该熟悉使用 &&
和 ||
等运算符的更复杂的表达式。我们最终会讨论这个问题,但不是在本文中。
总而言之,这是我知道的一个主题,我们将讨论该主题,但不是今天。
对于那些更有编程经验的人来说,您可能会熟悉三元运算符。
我们不会在这个特定的系列文章中讨论这一点,因为它超出了我们想要涵盖的范围;但是,如果您喜欢冒险,并且正在寻找一种更简洁的方法来编写简单的 if/else
语句,请查看 PHP 手册中的三元运算符。
switch/case
语句话虽如此,在继续下一个主题之前,我们还需要了解另一种类型的条件条件。
这个特定的结构仍然属于条件语句;但是,我认为您会发现它的使用频率低于其 if/else
对应项。
如标题所示,这称为 switch/case
语句。尽管我个人认为该语言使其变得更加复杂,但控制流通过评估的方式与我们已经看到的没有太大不同。
就像我们对 if/else
语句所做的那样,我们首先看一下 switch/case
的结构,然后我们看一下几个简单的例子。
<?php switch ( condition ) { case 'value': // do action break; case 'another value': // do a different action break; default: // perform a default action break; }
关于这种特定类型的条件,首先要注意的是,评估发生在一个位置:在 switch
语句旁边的代码块顶部。 p>
这里,评估发生一次,然后每个后续 case
语句决定采取哪个操作。我们将讨论的每个语句中还包含一个 break
语句,并且在我们将要讨论的底部还有一个 default
代码块也在文章末尾进行讨论。
但在我们执行任何操作之前,让我们设置一个稍微更实际的示例,说明基本 switch/case
语句的样子。
假设我们有一个值 $first_name
,然后我们希望根据此人的名字采取特定的操作过程。在此示例中,我们将根据某人的名字设置其电子邮件地址。如果我们无法识别该人的名字,那么我们会将值设置为 null
。
当然,这是一个有点做作的例子,但它会证明这一点:
<?php $persons_name = 'Tom'; $email_address = ''; switch ( $persons_name ) { case 'Tom': $email_address = 'tom@acme-server.com'; break; case 'David': $email_address = 'david@acme-server.com'; break; default: $email_address = NULL; break; }
让我们看一下上面示例中的控制流程:
$persons_name
定义为“Tom”,并将 $email_address
初始化为空字符串。$persons_name
传递给 switch 语句进行评估。$persons_name
。$persons_name
的值,因此 $email_address
将设置为“tom@acme-server.com”李>
如果我们将“David”作为 $persons_name
传递,则 $email_address
将设置为“david@acme-server.com”。 p>
接下来,如果我们要传递除“Tom”或“David”之外的任何其他名称,则 $email_address
将设置为 NULL
.还需要注意的是 switch/case
区分大小写。这意味着如果您传递“tom”而不是“Tom”,那么它们将被视为不同的情况。
最后,请注意每个 case
以 break
语句结尾。这很重要,因为 break
指示代码跳出 switch/case
语句并继续处理接下来出现的任何代码。
了解这一点非常重要:如果您忘记了 break 语句,那么它将立即落入下一个 case 语句,这显然会产生不稳定的结果(例如设置错误的 $email_address
)。
您可以利用这一点来发挥自己的优势的一个示例如下:
<?php $persons_name = 'Tom'; $email_address = ''; switch ( $persons_name ) { case 'tom': case 'Tom': $email_address = 'tom@acme-server.com'; break; case 'David': $email_address = 'david@acme-server.com'; break; default: $email_address = NULL; break; }
在上面的示例中,我们为“Tom”定义了小写或首字母大写的情况,并演示了代码如何进入下一个 case
语句。
但是有一个更好的方法可以让它更加防弹:
<?php $persons_name = 'Tom'; $email_address = ''; switch ( strtolower( $persons_name ) ) { case 'tom': $email_address = 'tom@acme-server.com'; break; case 'david': $email_address = 'david@acme-server.com'; break; default: $email_address = NULL; break; }
请注意,这需要 PHP 函数 strtolower
来强制传入的 $persons_name
完全小写。这使我们能够进一步完善我们的案例陈述。
在本文中,我们研究了 PHP 中可用的两组控制结构中的第一组。不,这些并不是面向对象编程的明确组成部分,但在我们真正开始讨论该范例的更多基础方面之前,我们需要了解允许我们编写面向对象代码的所有细节。
为此,我们将在下一篇文章中通过查看循环来继续讨论控制结构。
之后,我们就可以将注意力转向函数了。对于那些熟悉过程式编程的人来说,函数并不是什么新鲜事。但是,如果您是面向对象编程的新手,那么有许多因素可以将它们与过程编程中的使用方式区分开来。
这就是下一组文章的路线图。与往常一样,欢迎提供反馈,我期待在下一篇文章中继续我们的讨论。
The above is the detailed content of Object-Oriented Programming in WordPress: Control Structures I. For more information, please follow other related articles on the PHP Chinese website!