Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP elseif或语句不起作用_Php_Regex_If Statement - Fatal编程技术网

PHP elseif或语句不起作用

PHP elseif或语句不起作用,php,regex,if-statement,Php,Regex,If Statement,我试图根据提交表单的内容定义一个变量,并对照MySQL数据库进行检查,但preg_匹配和$variable匹配不能一起工作。 他们独立工作,但不是一起工作 我已经在每个步骤中打印出每个变量,并证明提交、比较和检索的数据始终存在,但在比较ifelse语句中的两个变量时,我无法定义变量:elseif(!preg_match(“/bt/I”,$zip)或$country!=='irland') 这个过程是: 表单提交->根据数据库比较变量->根据数据库比较输出变量 提交表格样本(示例): fbid(1

我试图根据提交表单的内容定义一个变量,并对照MySQL数据库进行检查,但preg_匹配和$variable匹配不能一起工作。 他们独立工作,但不是一起工作

我已经在每个步骤中打印出每个变量,并证明提交、比较和检索的数据始终存在,但在比较ifelse语句中的两个变量时,我无法定义变量:elseif(!preg_match(“/bt/I”,$zip)或$country!=='irland')

这个过程是: 表单提交->根据数据库比较变量->根据数据库比较输出变量

提交表格样本(示例):

fbid(12345678901)

城市(贝尔法斯特)

国家(联合王国)

拉链(BT1 1DS)

这是导致问题的(减少)代码:

    $country = $location['country']; //good value: Ireland / bad value: Italy(or empty)
    $zip = $location['zip']; //good value: BT1 1DS / bad value: 00(or empty)

    if ($fbid == $id) { //this works
    $confirmpage = '<h3>Sorry, this page is already in the table.</h3>';
    } elseif ( !preg_match("/bt/i",$zip) or $country !== 'Ireland' ) { //confirm location
    $confirmpage = '<h3>This page is not in Northern or Southern Ireland</h3>';
    } else { //success, page can be submitted
    $confirmpage = '<h3>Confirm Page</h3>';
    }
当我删除此语句时,脚本的其余部分工作正常。 如果选项1是否定的,在该声明到位后,结果始终是选项2,即使提交的表格包括zip开头的BT或爱尔兰作为国家:

    $confirmpage = '<h3>This page is not in Northern or Southern Ireland</h3>';
$confirmpage='此页面不在北爱尔兰或南爱尔兰';
试试看


我创建了一个基本的php页面代码,并将其复制到您的代码中,然后手动将$country和$zip设置为好值或坏值。它按预期工作,因此表明您的预匹配没有问题。

通过更改检查执行方式对其进行排序。 我用ifelse来检查帖子,而不是:if“condition”或“condition2”:

    $confirmpage = '<h3>This page is not in Northern or Southern Ireland</h3>';
    if ($fbid == $id) {
$confirmpage = '<h3>Sorry, this page is already in the table.</h3>';
} elseif ( preg_match("/bt/i",$zip) ) { //BT post code present
$confirmpage = $confirm;
} elseif ($country == 'Ireland') { // //Ireland present
$confirmpage = $confirm;
} else { //confirm page
$confirmpage = '<h3>This page is not in Northern or Southern Ireland</h3>';
$confirmpage='此页面不在北爱尔兰或南爱尔兰';
如果($fbid==$id){
$confirmpage='抱歉,此页已在表中。';
}elseif(preg_match(“/bt/i”,$zip)){//bt邮政编码存在
$confirmpage=$confirm;
}elseif($country=='irland'){///爱尔兰出席
$confirmpage=$confirm;
}else{//确认页面
$confirmpage='此页面不在北爱尔兰或南爱尔兰';

运算符
|
不同。请尝试
|
。@Gumbo:从链接的页面上可以看到,
|
的优先级都低于
!==
,所以这在这里并不重要。您的问题不清楚预期结果是什么。您的条件是:如果“bt”不在zip中,或者如果他们没有选择爱尔兰作为国家,have可以翻译为“Show‘This page not in Northern or south irland’”。我认为你的逻辑是颠倒的,这两个都应该是积极的?
preg_match
实际上返回匹配数(0或1)。当然,您可以匹配
0==false
,但确实没有必要,并且如果发生错误(无效模式等),则
preg\u match
将返回
false
,您确实应该只使用
==
将其与
false
进行比较,以避免混淆。谢谢。这已经完成了一半,错误条目和爱尔兰条目输出正确,但正确的邮政编码返回错误条目。
elseif ( preg_match("/bt/i",$zip) == 1 || $country != 'Ireland' )
    $confirmpage = '<h3>This page is not in Northern or Southern Ireland</h3>';
    if ($fbid == $id) {
$confirmpage = '<h3>Sorry, this page is already in the table.</h3>';
} elseif ( preg_match("/bt/i",$zip) ) { //BT post code present
$confirmpage = $confirm;
} elseif ($country == 'Ireland') { // //Ireland present
$confirmpage = $confirm;
} else { //confirm page
$confirmpage = '<h3>This page is not in Northern or Southern Ireland</h3>';