第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在php和print中使用if multiple語(yǔ)句創(chuàng)建變量

如何在php和print中使用if multiple語(yǔ)句創(chuàng)建變量

PHP
Cats萌萌 2023-03-26 14:21:43
我需要以下代碼的幫助$selected_payment_option =  'if($payment_mode == "cod"){ echo "Cash on Delivery ";}                                        if($payment_mode == "stripe"){ echo "Stripe";}                                        if($payment_mode == "bank"){ echo "Bank";}';并希望打印為 $selected_payment_option
查看完整描述

2 回答

?
慕萊塢森

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

有很多方法可以做到這一點(diǎn)

使用 if-else:


$payment_mode = "bank";

if($payment_mode == "cod")

  $selected_payment_option = "Cash on Delivery";

elseif($payment_mode == "stripe")

  $selected_payment_option = "Stripe";

elseif($payment_mode == "bank")

  $selected_payment_option = "Bank";

else

  $selected_payment_option = "your default value";

print($selected_payment_option)

// output: Bank

使用開關(guān):


$payment_mode = "cash";

switch($payment_mode) {

  case "cod": $selected_payment_option = "Cash on Delivery"; break;

  case "stripe": $selected_payment_option = "Stripe"; break;

  case "bank": $selected_payment_option = "Bank"; break;

  default: $selected_payment_option = "your default value";

}

print($selected_payment_option);

// output: your default value

使用三元運(yùn)算符:


$payment_mode = "stripe";

$selected_payment_option = $payment_mode == "cod" ? "Cash on Delivery "

  : ($payment_mode == "stripe" ? "Stripe" : ($payment_mode == "bank" ? "Bank" 

  : "your default value"));

print($selected_payment_option);

// output: Stripe

使用邏輯運(yùn)算符:


$payment_mode = "bank";

$selected_payment_option = "your default value";

$payment_mode == "cod" && ($selected_payment_option = "Cash on Delivery") ||

  $payment_mode == "stripe" && ($selected_payment_option = "Stripe") ||

  $payment_mode == "bank" && ($selected_payment_option = "Bank");

print($selected_payment_option);

// output: Bank

使用關(guān)聯(lián)數(shù)組:


$payment_modes = array("cod"=> "Cash on Delivery", "stripe"=> "Stripe", "bank" => "Bank");

$payment_mode = "cod";

$selected_payment_option = array_key_exists($payment_mode, $payment_modes) ?

  $payment_modes[$payment_mode] : "your default value";

print($selected_payment_option);

// output: Cash on Delivery


查看完整回答
反對(duì) 回復(fù) 2023-03-26
?
PIPIONE

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊

請(qǐng)為此類代碼使用開關(guān)。它還具有捕獲 $payment_mode 未設(shè)置值的情況的好處。


function getPaymentOption($payment_mode) {

    switch ($payment_mode) {

        case "cod":

            return "Cash on Delivery";

            break;

        case "stripe":

            return "Stripe";

            break;

        case "bank":

            return "Bank";

            break;

        default:

            return "unknown";

    }

}

像這樣使用:


$selected_payment_option = getPaymentOption($payment_mode);


查看完整回答
反對(duì) 回復(fù) 2023-03-26
  • 2 回答
  • 0 關(guān)注
  • 128 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)