PHP - 允许多个响应

时间:2014-06-30 20:32:43

标签: php

我正试图用PHP来了解亚马逊api。目前我在另一个程序员的程序中有这个代码:

        if ($pxml->Items->Item->Offers->Offer->OfferListing->Availability == 'Usually dispatched within 24 hours')
            $stock = ($pxml->Items->Item->OfferSummary->TotalNew > 0) ? '1' : '0';
        else
            $stock = 0;

我怎样才能做到这一点而不是Usually dispatched within 24 hours的回复,我们也允许Usually ships in 1-2 business days的回复。

我是否需要使用or指示符?

2 个答案:

答案 0 :(得分:1)

或者......你实际上改进了你的php,以便将来更容易理解和编辑:

$val = $pxml->Items->Item->Offers->Offer->OfferListing->Availability; 
if ($val == 'Usually dispatched within 24 hours'){
     //do X ;
 }
 elseif($val == 'Usually ships in 1-2 business days'){ 
     //do Y ;
 }

这样,您可以根据需要添加任意数量的elseif条件,或者更改XML值,而不必反复重复更改。如果它太大了,那就试试吧。

答案 1 :(得分:0)

if ($pxml->Items->Item->Offers->Offer->OfferListing->Availability ==
'Usually dispatched within 24 hours' ||
$pxml->Items->Item->Offers->Offer->OfferListing->Availability ==
'Usually ships in 1-2 business days'){

使用logical operator表示“或”(||) - 如果您要在“24小时内”和“1-2个工作日内”执行相同的操作。否则,

 if ($pxml->Items->Item->Offers->Offer->OfferListing->Availability ==
 'Usually dispatched within 24 hours'){
     //do X
 }
 elseif($pxml->Items->Item->Offers->Offer->OfferListing->Availability ==
 'Usually ships in 1-2 business days'){ 
     //do Y
 }

允许您根据不同的回复做不同的事情