簡體   English   中英

PHP elseif或語句不起作用

[英]PHP elseif or statement not working

我正在嘗試根據提交的表單的內容定義變量,並對照MySQL數據庫進行檢查,但preg_match和$ variable匹配將無法一起使用。 他們獨立工作,但不一起工作。

我已經在每個步驟中打印出每個變量,並證明了提交,比較和檢索的數據始終存在,但是在ifelse語句中比較兩個變量時我無法定義該變量:elseif(!preg_match(“ / bt / i”,$ zip )或$ country!=='愛爾蘭')

過程是:表單提交->將變量與數據庫進行比較->輸出變量取決於數據庫的比較。

表單提交示例(示例):

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>';
    }

代碼不起作用是:

        elseif ( !preg_match("/bt/i",$zip) or $country !== 'Ireland' )

當我刪除此語句時,腳本的其余部分都可以正常工作。 如果選項1為否,則使用此語句,結果始終為選項2,即使提交的表單在zip開頭包含BT或愛爾蘭作為國家/地區也是如此:

    $confirmpage = '<h3>This page is not in Northern or Southern Ireland</h3>';

嘗試

elseif ( preg_match("/bt/i",$zip) == 1 || $country != 'Ireland' )

我創建了一個基本的php頁面代碼並將其復制到您的代碼中-然后手動將$ country和$ zip設置為好值或壞值-它可以按預期工作,因此建議您的preg_match沒問題。

通過更改檢查方式對其進行了排序。 代替:如果是“ condition”或“ condition2”,我使用了ifelse來檢查正則表達式:

    $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>';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM