简体   繁体   中英

How to Assign true condition's value to a variable outside of if else condition in laravel?

I am trying to assign some value to a variable named $bonus in if-else conditions and then I am trying to call $bonus outside of the if-else condition so the true condition's variable should be called automatically. I have tried this code but it is giving me an error on debugging mode of laravel

Error:

Undefined variable: bonus

Code:

<?php
if ($data->lend_amount >= 50 && $data->lend_amount <= 500) {
    $bonus = ($data->lend_amount * 4) / 100;
} else if ($data->lend_amount >= 500 && $data->lend_amount <= 50000) {
    $bonus = ($data->lend_amount * 5) / 100;
}

if ($user_b_update) {
    Transaction::create([
        'user_id' => $data->user_id,
        'trans_id' => rand(),
        'time' => Carbon::now(),
        'description' => 'Matching Bonus Of ' . $bvp . ' Users',
        'amount' => $bonus,
        'new_balance' => $newacc_bal,
        'new_earning' => $newbal,
        'type' => 4,
        'charge' => 0
    ]);
}

You have to define $bonus

$bonus = 0;

if ($data->lend_amount >= 50 && $data->lend_amount <= 500) {
  $bonus = ($data->lend_amount * 4) / 100;
} else if ($data->lend_amount >= 500 && $data->lend_amount <= 50000) {
  $bonus = ($data->lend_amount * 5) / 100;
}

Or set default value in the last else block

if ($data->lend_amount >= 50 && $data->lend_amount <= 500) {
  $bonus = ($data->lend_amount * 4) / 100;
} else if ($data->lend_amount >= 500 && $data->lend_amount <= 50000) {
  $bonus = ($data->lend_amount * 5) / 100;
} else {
  $bonus = 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM