简体   繁体   English

如何在PHP中使用If语句合并两个参数

[英]How Can I Combine two Arguments Using If Statements in PHP

I really need your help here. 我真的需要您的帮助。 I'd like to know how I can use two arguments greater than(>) and less than (<) in a single if statement. 我想知道如何在单个if语句中使用两个大于(>)和小于(<)的参数。 I was trying to do this in my code below, but I keep on getting an error message on the 'else' statement. 我试图在下面的代码中执行此操作,但是我一直在'else'语句中收到错误消息。

<?php

$value1="7";
if($value1<78);
if($value1>7);
{print"Yes, the answer is above 7 but below 78";} 

else 

{print"that's not correct";}


?>

Any help would be much appreciated, thanks. 任何帮助将不胜感激,谢谢。

Use && : 使用&&

if($value1 < 78 && $value1 > 7){
    print"Yes, the answer is above 7 but below 78";
} 
else {
    print"that's not correct";
}

See: Control Structures and Logical Operators 请参阅: 控制结构逻辑运算符

<?php
$value1="7";
if($value1<78 && $value1>7)
{
    print "Yes, the answer is above 7 but below 78";
} 
else 
{
    print"that's not correct";
}
?>

As others have mentioned, use PHPs && (and) to combine logic. 正如其他人提到的,使用PHP && (和)组合逻辑。

As an alternate to if/else methods listed here, you can use PHP's ternary operators . 作为此处列出的if/else方法的替代方法,您可以使用PHP的三元运算符

Something like this: 像这样:

echo ($value1<78 && $value1>7)
    ? "Yes, the answer is above 7 but below 78."
    : "That's not correct";

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

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