简体   繁体   English

在使用if语句循环后如何具有else语句?

[英]How to have an else statement after a loop with an if statement?

I am trying to make a registration system with text files and I need help with using an else statement after the loop that checks if the username is taken. 我正在尝试使用文本文件创建注册系统,并且在检查用户名是否被使用的循环之后,我需要使用else语句的帮助。

I am generally just trying to find out how to have an else statement after a loop with an if statement, if I find out that out my problem is basically solved. 如果我发现问题基本上已经解决,我通常只是想尝试在使用if语句循环后如何具有else语句。 Here is the code: 这是代码:

while($i < count($logindata)-1) {
  if ($_POST['username'] == $user[$i]['username']) {
    set_message(" That username is taken", "danger");
  }
  $i++;
}
else {
    if (!empty($_POST['username']) && !empty($_POST['password'])) {
        file_put_contents('logininformation.txt', $_POST['username'] . "?=%&$#@[}[}+-789409289746829" . $_POST['password'] . "\n", FILE_APPEND);
        set_message("Account created!", "success");
    } else {
      set_message(" You have not put in a username and/or password","danger");
    }
}

I expect to be able to have an else statement after the loop and it working. 我希望在循环后能够有一个else语句,并且它可以工作。

A loop is not a condition, therefore it does not have an else part either. 循环不是条件,因此也不包含else部分。 It is correct that the loop runs while the condition evaluates to true, but as soon as the condition does not evaluate to true, the loop is ended. 循环在条件评估为真时运行是正确的,但是条件不评估为真时,循环就结束了。

Therefore, to check whether the loop was not triggered at all, you have to find a different way, eg write a condition on its own. 因此,要检查循环是否根本未触发,您必须找到另一种方式,例如,自己编写条件。

For the sake of argument, you COULD save a flag and evaluate that afterwards, but in most cases I would not recommend that : 为了讨论的方便,你COULD保存一个标志,事后评价说,但在大多数情况下, 我不会建议

$i = 0;
$loopDidRun = false;
while ($i < 10) {
    $i++;
    $loopDidRun = true;
}

if (!$loopDidRun) {
    echo "loop did not run, therefore the 'else case', but not really";
}

Your logic is severely flawed. 您的逻辑存在严重缺陷。

$failed=false;

if(empty($_POST['username']) || empty($_POST['password'])){
    $failed=true,
    set_message(" You have not put in a username and/or password","danger");
}else{
    while($i<count($logindata)-1){
        if($_POST['username']==$user[$i]['username']){
            $failed=true;
            set_message("That username is taken","danger");
            break;
        }
        $i++;
    }
}

if(!$failed){
    file_put_contents('logininformation.txt',$_POST['username']."?=%&$#@[[}+-789409289746829".$_POST['password']."\n",FILE_APPEND);
    set_message("Account created!","success");
}

However all I am doing here is fixing bad code. 但是,我在这里所做的只是修复错误的代码。 Before anything you need to filter the $POST input given to disallow just any input, passwords etc. should not be stored in plain text, and this is not the proper way of creating a factory for this. 在需要过滤$POST输入以禁止任何输入,密码等之前,不应将其存储为纯文本格式,这不是为此创建工厂的正确方法。 You should find better, and secure, examples online and work from them. 您应该在线找到更好,更安全的示例,并从中进行工作。

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

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