簡體   English   中英

php中的字符串比較,如果條件不正常

[英]string comparison in php if condition is not working properly

我想比較一個字符串,但它不起作用。

首先我把它放在我的腳本中並存儲在一個變量中,之后我將變量存儲在會話中。

這是我的代碼......

<?php
    $currencyName1 = "<script>document.write(data);</script>";

    $_SESSION["currencyVal"] = $currencyName1;

    $price = strip_tags($this->session->userdata('currencyVal'));
    $price1 = substr($price,14,17);

    if (strcmp($price,"SAR")) {
        echo "SAR"; 
    } else {
        echo "USD"; 
    }
?>

使用cookie來獲取正確的字符串在$ _SESSION中,您可以使用腳本標記獲取值<script>document.write(data);</script>

同意@ lehmanad1,你需要像strcmp($ price1,“SAR”)那樣進行比較,並記住strcmp返回結果如下。

strcmp returns:
0 - if the two strings are equal
<0 - if string1 is less than string2
>0 - if string1 is greater than string2

因此,在您的情況下,如果strcmp($ price1,“SAR”)變為true,則if條件返回0,因此將執行其他條件。

你需要嘗試以下方法。

if (strcmp($price1,"SAR")==0) {
    echo "SAR"; 
} else {
    echo "USD"; 
}

暫無
暫無

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

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