else不在php中执行

else不在php中执行,php,if-statement,Php,If Statement,如果我将所有字段留空,则代码blow不会显示else消息 <form action="index.php" method="POST"> <textarea name="input_area" rows="6" cols="20"></textarea><br/><br/> Search<input type="text" name="find"/><br/><br/> Rep

如果我将所有字段留空,则代码blow不会显示else消息

<form action="index.php" method="POST">
    <textarea name="input_area" rows="6" cols="20"></textarea><br/><br/>
    Search<input type="text" name="find"/><br/><br/>
    Replace<input type="text" name="replace" /><br/><br/>
    <input type="submit" value="Find and replace"/>
</form>

这些值将在$u POST中设置,但它们将为空。这样的检查在所有情况下都有效,即使在某些情况下,有人修改了您的html并尝试了一些有趣的东西

<?php
  $text = isset($_POST['input_area']) ? $_POST['input_area'] : "";
  $find = isset($_POST['find']) ? $_POST['find'] : "";
  $replace = isset($_POST['replace']) ? $_POST['find'] : "";

  if($text != "" && $find != "" && $replace != ""){
    $new_str=str_replace($find,$replace,$text);
    echo $new_str;  
  }else{
    echo 'FILL ALL THE FIELDS';       
  }
?>

这句话是问题的根源

if(isset($_POST['input_area'])&&($_POST['find'])&&($_POST['replace']))
正确的答案是

     if(isset($_POST['input_area'])&&isset($_POST['find'])&&isset($_POST['replace']))

阿曼有一个很好的答案。您可能需要检查用户是否刚刚输入了空格。请查找ctype_space($str)以了解这一点。您还可以为此目的调整strlen,并修剪掉可能出现在输入(或全部)开头或结尾的空白。。。如果希望代码结构看起来相同

if (  strlen(trim($_POST['input_area']))>0 && etc. 

var_转储您的变量,并查看它们实际包含的内容。将
var\u dump($\u POST)
放在代码上方,看看它显示了什么。@aynber evrything在我插入值时工作正常,只有else语句不工作working@aman这是我的观点。如果else从未发生过,那么你需要检查你的变量,看看为什么,我收回它。你永远也不会有其他机会,因为你不会经历第一个if。把else和第一个if放在一起,而不是第二个if。
if (  strlen(trim($_POST['input_area']))>0 && etc.