根据变量的值运行数组

时间:2019-06-27 03:05:57

标签: php

我有两个数组。如果变量“ purchase”为真,我只希望运行数组“ one”。如果该变量为false,则仅运行数组“ two”

我是PHP的新手,如果术语错误,请原谅我。我认为它将涉及if / else语句

$purchase = "----";

// This is array "one"
$charge = \Stripe\Charge::create(array(
"customer" => $customer->id,
"description" => "Single Purchase")
));

// This is array "two"
$subscription = \Stripe\Subscription::create(array(
"customer" => $customer->id,
"plan" => $_POST['subscription'],
"coupon" => $_POST['coupon'],
"quantity" => $_POST['quantity'],
"description" => "Subscription")
));

1 个答案:

答案 0 :(得分:1)

if($purchase){
    // This is array "one"
    $charge = \Stripe\Charge::create(array(
        "customer" => $customer->id,
        "description" => "Single Purchase")
    ));
} else{
    // This is array "two"
    $subscription = \Stripe\Subscription::create(array(
        "customer" => $customer->id,
        "plan" => $_POST['subscription'],
        "coupon" => $_POST['coupon'],
        "quantity" => $_POST['quantity'],
        "description" => "Subscription")
    ));
}

如果$purchase变量为true,则数组1将运行,否则数组2将运行