2017-12-07 7 views
0

これはif/elseif文です。$ DiscountDescriptionが空でない場合は、 "e"、 "E"、 "A"のいずれかであり、12文字または10文字のいずれかです。他の2つのシナリオには、18文字と2つのダッシュと9文字の数字があります。PHP文字列の開始文字と文字列の長さを決定し、結果に基づいて変数に値を代入します。

コードはわかりましたが、動作しているようです。何か案は?

$DiscountDescription = $_order->getDiscountDescription(); 

if ($DiscountDescription != '') { 
    if (substr($DiscountDescription,0,1 == "e") && strlen($DiscountDescription == 12)){ 
     $_order->setDiscountDescription('Gift Cards ' . $DiscountDescription); 
    } 
    elseif (substr($DiscountDescription,0,1 == "E") && strlen($DiscountDescription == 10)){ 
     $_order->setDiscountDescription('Gift Cards ' . $DiscountDescription); 
    } 
    elseif (substr($DiscountDescription,0,1 == "A") && strlen($DiscountDescription == 10)){ 
     $_order->setDiscountDescription('Gift Cards ' . $DiscountDescription); 
    } 
    elseif (strlen($DiscountDescription == 18) && substr_count($DiscountDescription,'-' == 2)){ 
     $_order->setDiscountDescription('Gift Cards ' . $DiscountDescription); 
    } 
    elseif (strlen($DiscountDescription == 9) && ctype_digit($DiscountDescription)){ 
     $_order->setDiscountDescription('Gift Cards ' . $DiscountDescription); 
    } 

    //$_order->setDiscountDescription('Gift Cards ' . $DiscountDescription); 
} 
+4

'strlen($ DiscountDescription == 10)' __必要がありますstrlen($ DiscountDescription)== 10'です。 __Same__ for 'substr_count' –

+1

@u_mulderそして' substr() '... – jeroen

+0

あなたはあなたのすべてを==の代わりに関数の中に入れています。substr_count($ var、" - ") == 2。とstrlen($ var)== 12 – hunijkah

答えて

0

誰かが、あなたが長さのチェックで比較を行っているように見えますが動作しません。長さを取得し、それを整数と比較したいとします。

ベストプラクティスとして、==ではなく、厳密な比較===も使用するようにしてください。

関連する問題