带变量的动态if语句?

时间:2013-05-03 10:04:09

标签: php regex if-statement

我正在尝试创建一个动态if语句。我想这样做的原因是因为我需要检查服务器端是否输入字段匹配我的正则表达式而不是空的。但是,我的一些输入字段可以在我的CMS中删除,这意味着会有更多/更少的输入字段。

理想情况下,我会在if语句中添加变量但是我不能100%确定是否允许这样做,所以也许我需要另一种方法来解决这个问题。这是我试过的:

if ($f_naw['streetname'] == 1)
{
    $streetname= $_POST['streetname']; //Used in INSERT query
    $cstreetname = " || $_POST['streetname'] == ''"; //Used to check if field is empty
    $pstreetname = " || !preg_match($streetnameReg,$_POST['streetname'])"; //Used to check if it matches my regex
}
else
{
    //These variables define variables if inputfields are not shown
    $streetname= ''; //No streetname means it's excluded in INSERT query
    $cstreetname = ''; //Not needed in check
    $pstreetname = ''; //Also not needed in check
}

// more of these if/else statements

if ($_POST['firstname'] == '' || $_POST['lastname'] == '' || $_POST['email'] == '' $cstreetname $cpostalcode $chometown $ctelnr $csex $cdateofbirth)
{
    echo 'One of the fields is empty.';
    header('refresh:3;url=index.php');
}
else
{
    //Regex check, after that more code
}

我的想法是检查前端是否显示特定字段,在这种情况下,我正在创建一些我想在if语句中粘贴的变量。

我收到错误,说服务器错误意味着我的php代码无效。

甚至可以制作动态if语句吗?如果是的话,我在哪个方面失败了?

非常感谢帮助!提前谢谢。

2 个答案:

答案 0 :(得分:0)

首先,由于看起来你需要将所有条件与||结合起来,你可以通过这样编写来纠正你的程序:

if ($f_naw['streetname'] == 1)
{
    $streetname= $_POST['streetname']; //Used in INSERT query
    $cstreetname = $_POST['streetname'] == ''; //Used to check if field is empty
    $pstreetname = !preg_match($streetnameReg,$_POST['streetname']); //Used to check if it matches my regex
}
else
{
    //These variables define variables if inputfields are not shown
    $streetname= ''; //No streetname means it's excluded in INSERT query
    $cstreetname = false; //Not needed in check
    $pstreetname = false; //Also not needed in check
}


if ($_POST['firstname'] == '' || $_POST['lastname'] == '' || $_POST['email'] == '' || $cstreetname || $cpostalcode || $chometown || $ctelnr || $csex || $cdateofbirth)
{
    echo 'One of the fields is empty.';
    header('refresh:3;url=index.php');
}

这可行,但它很笨重。一个更好的解决方案是使用一个数组(让它命名为$errors,它会动态填充因验证字段而产生的错误。像这样:

$errors = array();

if ($f_naw['streetname'] == 1)
{
    $streetname= $_POST['streetname']; //Used in INSERT query
    if ($streetname == '') {
        $errors[] = 'Streetname cannot be empty.'; // message is optional
    }
    if (!preg_match($streetnameReg,$streetname)) {
        $errors[] = 'Streetname is invalid.'; // message is optional
    }
}

然后:

if ($errors) {
    echo 'There are errors with the data you submitted.';
    header('refresh:3;url=index.php');
}

如果您提供了人类可读的错误消息,您还可以安排它们显示,以便用户知道他们需要修复的内容。当然,你可以使用这种技术的很多变化 - 例如按字段对错误消息进行分组,以便您只为每个字段显示一个错误。

答案 1 :(得分:0)

如果你想检查空的$ _POST字段,你可以做类似的事情

$error = False;
foreach($_POST as $k => $v)
{
    if(empty($v))
    {
        $error .= "Field " . $k . " is empty\n";
    }
}

if(!$error)
{
    echo "We don't have any errrors, proceed with code";
}
else
{
    echo "Ops we have empty fields.\n";
    echo $error;
}

在您确定所有字段都不为空之后,您可以执行其他操作。