简体   繁体   English

php语法-在语句中使用'or'和'&&'

[英]php syntax - using 'or' and '&&' in statement

This is for a text field password input. 这是用于文本字段密码输入的。 Here is my code: 这是我的代码:

$vaar is what is entered into that field. $vaar是在该字段中输入的内容。

if(($vaar !='pass01' or 'pass02') && (!empty($vaar))) {

I can't figure out the correct syntax. 我找不到正确的语法。 I've tried || 我尝试过|| , or , xor . or xor I've also tried placing 'pass01' and 'pass02' in their own ( ) 's. 我也尝试将'pass01''pass02'放在自己的( )

What I want it to do is this: 我想要它是这样的:

If $vaar isn't 'pass01' or 'pass02' and $vaar is not empty then do this: 如果$vaar不是'pass01''pass02'并且$vaar不为空,则执行以下操作:

just a syntax error, but I can't figure it out! 只是一个语法错误,但我无法弄清楚!

You're looking for: 您正在寻找:

if( !empty($vaar) && ($vaar != 'pass01' && $vaar != 'pass02')) {

Note that I've put the empty() check first, which will short-circuit the evaluation should $vaar in fact be empty. 请注意,我首先放置了empty()检查,如果$vaar实际上为空,这将使求值短路。

Edit: To better reflect the OP's wording (and logic), this is identical to the following, because of Demorgan's law. 编辑:为了更好地反映出OP的措辞(和逻辑),这与以下内容相同,这是由于Demorgan定律所致。 ): ):

if( !empty($vaar) && !($vaar == 'pass01' || $vaar == 'pass02')) {

Reads: If $vaar is not empty and $vaar is not pass01 or pass02 读取: 如果$vaar不为空并且$vaar不是pass01pass02

if(($vaar !== 'pass01' and $vaar !== 'pass02') and (!empty($vaar))) {
   // ...
}

Alternatively: 或者:

if(($vaar !== 'pass01' && $vaar !== 'pass02') && (!empty($vaar))) {
   // ...
}

What you actually mean is: 您的实际意思是:

If $vaar is not equal to "pass01" AND $vaar is not equal to "pass02" AND $vaar is not empty, then ... 如果$vaar不等于“ pass01”并且$vaar不等于“ pass02”并且$vaar不为空,则...

Note that I have kept your original parentheses in there, but since they are all AND, you may remove them, like this: 请注意,我将原始括号保留在其中,但是由于它们都是AND,因此您可以将其删除,如下所示:

if(!empty($vaar) && $vaar !== 'pass01' && $vaar !== 'pass02') {
   // ...
}

Here, I have also moved the empty language construct to the beginning of the conditional, as if the variable is empty then the conditional will evaluate the false immediately, preventing an E_NOTICE error for Undefined variable $vaar if the variable is undefined. 在这里,我还将empty语言构造移至条件的开头,好像变量为空,则条件将立即评估为false,如果Undefined variable $vaar ,则防止E_NOTICE错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM