PHP if语句

The if statement is one that is central to all scripting languages. In PHP, the basic syntax is:

if语句是所有脚本语言的核心。 在PHP中,基本语法为:

if (condition) { result; }

Our condition goes within parentheses, while the result – what we want to have happen if the condition is met – goes within curly braces. Our result could be many lines long; and, as we’ll see shortly, it need not be PHP.

我们的条件放在括号内,而结果(如果满足条件,我们希望发生的事情)在花括号内。 我们的结果可能会长很多行。 并且,正如我们稍后将看到的,它不必是PHP。

The simplest statement one could make would be something like the following:

一个人可以做的最简单的声明如下:

<?php if ($_POST['userName'] == "Nom") { 
	echo "<p>Welcome, mighty SuperUser!</p>";
} ?>

It’s very important to note the double equal sign (==), known as the comparison operator. A single equals sign sets a variable to a particular value; a double equals sign compares a variable to a value.

注意双等号( == ),即比较运算符,这一点非常重要。 单个等号变量设置为特定值。 等号变量与值进行比较

While that example works, it’s rare that you would be testing against a single, known value. It’s more likely that you simply want to know if the username field had been filled out with anything at all:

尽管该示例有效,但很少会针对单个已知值进行测试。 您更有可能只是想知道username段是否全部填满:

<?php if ($_POST['userName'] != "") { 
	echo "<p>You’ve filled the field</p>”;
} ?>

We’ve used the not equal to (!=) operator to compare what has been entered into the field to blank (two quote marks right next to each other). If that is the case, we echo out a paragraph.

我们使用了不等于 ( != )运算符来比较在字段中输入的内容是否为空白(两个引号彼此相邻)。 如果是这样,我们echo一段。

The logic could be reversed to make the opposite statement:

逻辑可以相反以做出相反的陈述:

<?php if ($_POST['userName'] == """) { 
	echo "<p>You have <em>not</em> filled the field</p>"; 
} ?>

This statement could also be written as:

该语句也可以写成:

<?php if (!$_POST['userName']) { 
	echo "<p>You have <em>not</em> filled the field</p>"; 
} ?>

One other alternative:

另一种选择:

<?php if (!isset($_POST['userName'])) { 
	echo "<p>You have <em>not</em> filled the field</p>"; 
} ?>

In this case, the not operator is moved before the variable. Essentially we are saying “if not variable” i.e. if the variable does not exist, do what follows as the action.

在这种情况下, not运算符将移动到变量之前。 本质上,我们说的是“如果不是变量”,即如果变量不存在,请执行以下操作。

If there are multiple variables to compare, we can use the and or or comparison operators:

如果有多个变量要比较,我们可以使用andor运算符:

<?php if (!$_POST['userName'] && !$_POST['password'] ) { 
	echo "<p>You have left both fields empty.</p>";
} ?>
<?php if (!$_POST['userName'] || !$_POST['password'] ){ 
	echo "<p>You have left <em>one</em> of these fields empty.</p>"; 
} ?>

The single best feature of PHP conditions is the fact that our result may be phrased as any kind of code or content. In our examples, we have been using echo to print our responses to the page. However, there is nothing dynamic about our responses, nor are we saying anything that couldn’t be stated in HTML. Returning to our first example, then, we could close the PHP early, and do the response in HTML:

PHP条件的唯一最佳功能是可以将我们的结果表达为任何类型的代码或内容。 在我们的示例中,我们一直在使用echo来打印对页面的响应。 但是,我们的响应没有动态变化,也没有说过HTML不能说明的任何内容。 回到我们的第一个示例,然后,我们可以及早关闭PHP,并以HTML进行响应:

<?php if ($_POST['userName'] == "Dudley") { ?>
	<p>Welcome, mighty SuperUser!</p>
<?php } ?>

This gives us the exact same results, but uses far less code. The only requirement is that we close the PHP after the opening brace, and reopen it to capture the closing brace. Between the two we could place anything: HTML, CSS, JavaScript, even more .

这给了我们完全相同的结果,但是使用的代码少得多。 唯一的要求是我们在打开括号后关闭PHP,然后重新打开以捕获关闭括号。 在这两者之间,我们可以放置任何内容: HTMLCSSJavaScript ,甚至更多

翻译自: https://thenewcode.com/185/The-PHP-if-Statement

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值