2014-07-02 88 views
0

我真的需要您的帮助。我想知道如何在单个if语句中使用两个大于(>)且小于(<)的参数。我试图在下面的代码中执行此操作,但我一直在'else'语句中收到错误消息。如何在PHP中使用If语句组合两个参数

<?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";} 


?> 

任何帮助将不胜感激,谢谢。

+0

可能重复[如何结合两个IF在PHP语句(http://stackoverflow.com/questions/5955678/how-to-combine-two-if-声明在PHP) – FuzzyTree

回答

1

使用&&

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

见:Control StructuresLogical Operators

+0

谢谢约翰,它的工作!对不起,我现在不能投票,没有足够的分数。 – user3799159

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

感谢你们! :) – user3799159

0

正如其他人所说,使用PHP的&&(和)为逻辑结合起来。

作为此处列出的if/else方法的替代方法,您可以使用PHP的ternary operators

事情是这样的:

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

感谢你也showdev! :) – user3799159