【问题标题】:if isset not returning false not finishing如果 isset 没有返回 false 没有完成
【发布时间】:2013-12-05 23:03:34
【问题描述】:

我在 php 中有一个 isset 检查,使用 if 语句检查各种函数是否返回 false。问题是,它不会在第一次错误返回后退出。

我确定我缺少 php 语法中的一些基本内容。

<?php
$name =  $_POST['name'];
$email = $_POST['email'];
$email2 = $_POST['email2'];

function nameCheck($name)
{
    if (strlen($name)<1 || strlen($name)>40)
    {
        return false;
    }
    else
    {
        return true;
    }
}

function emailCheck($email)
{
    $regex = "/^[*\w]{1,25}+@[*\w]{1,20}+\.[*\w]{1,10}$/";
    if (preg_match($regex, $email))
    {
        return true;
    }
}

function emailMatch($email, $email2)
{
    if ($email === $email2)
    {
        return true;
    }
    else
    {
        return false;
    }
}

if (isset($_POST['submit']))
{
    if (!nameCheck($name))
    {
        echo '<script type="text/javascript"> alert("Fail name!")</script>';
        return false;
    }
    elseif (!emailCheck($email))
    {
        echo '<script type="text/javascript"> alert("Fail email check!")</script>';
        return false;
    }
    elseif (!emailMatch($email,$email2))
    {
        echo '<script type="text/javascript"> alert("Fail email match!")</script>';
        return false;
    }
    else
    {
    mail ("email@email.com", "this", "message");
    echo '<script type="text/javascript"> alert("Success!")</script>';
    }
}
?>

我正在使用警告框向代码发送文本。 不仅会显示每个警报框,甚至会显示成功的警报框! 我究竟做错了什么?交易

这只是我为了考试而玩的代码,我实际上不需要在网站上使用它,所以只是练习。

【问题讨论】:

  • return truel; 是错字吗?
  • 另外——如果你的主代码中有一个return——它应该做什么?它没有返回的调用函数。
  • 您是否只想在您的某个函数返回 false 时停止执行代码?在这种情况下,您需要将那些return 调用与die 切换:if (! nameCheck) { die('&lt;script...'); } 应该这样做。
  • 我会添加一个答案 - 还有更多空间:D

标签: php if-statement return isset


【解决方案1】:

你现在的代码是有效的:

if (! check) {
    echo();
    return;
}

if (! check) {
    echo();
    return;
}

但是函数之外的return 不会做任何事情 - PHP 无处可将控制权返回

您需要做的是切换这些检查,以便将它们格式化为:

if (isset($_POST['submit']))
{
    if (!nameCheck($name))
    {
        die('<script type="text/javascript"> alert("Fail name!")</script>');
    } 
    elseif
    ......
    } 
 else
    {
    mail ("email@email.com", "this", "message");
    echo '<script type="text/javascript"> alert("Success!")</script>';
    }

使用die 将打印出字符串的内容,并立即停止执行您的代码。

关于功能……

如果你要使用它们的返回值,你需要确保它们总是返回一些东西。目前,emailCheck 是:

function emailCheck($email)
{
    $regex = "/^[*\w]{1,25}+@[*\w]{1,20}+\.[*\w]{1,10}$/";
    if (preg_match($regex, $email))
    {
        return true;
    }
}

如果没有匹配,则返回null;但是,当您在 if 语句中检查时,它将评估为 false,因此您的检查不会注意到。当您调用return 时,您的函数会立即停止运行,并且控制权会返回给调用它的代码,因此您可以将检查函数编写为:

function myCheck($parameter) {
    if ($checkhere == 'something') {
        return true;     // returns true to the calling function
                         // stops running the code in this function
    } 
    // The only way this will be run is if the check has failed, so we 
    // don't need an else
    return false;
}

这与使用 else 完全相同,但输入的内容略少 - 选择哪种方式会有所不同。

【讨论】:

  • 如果我理解正确,你的函数很好 - 他们根据标准检查参数,并返回 truefalseemailCheck 除外 - 这没什么如果检查失败,则如果通过,则为 true,但我怀疑这是另一个错字)
  • 我在问题中添加了一些注释 - 这有帮助吗?
猜你喜欢
  • 2013-01-09
  • 2011-02-20
  • 1970-01-01
  • 2020-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-24
  • 1970-01-01
相关资源
最近更新 更多