-1

I am trying pass a value inside stripe function function calculateOrderAmount(array $items): int using variable declared outside function, but its not working. But if I declare variable inside the function function calculateOrderAmount(array $items): int its working. Its confusing.

What is working


function calculateOrderAmount(array $items): int {
   // assign session amount to a variable
   $amount = 500;
    return $amount;
}

What is not working and what I want

$amount = 500;
function calculateOrderAmount(array $items): int {
   // assign session amount to a variable
    return $amount;
}

I am trying to pass a value to the function from outside, kindly help

1 Answers1

0

If you want your $amount variable to be defined outside the function as a global variable. Then you should tell the function to "import" it like so:

$amount = 500;
function calculateOrderAmount(array $items): int {
   global $amount;
   // assign session amount to a variable
   return $amount;
}

But I wouldn't recommend using global variables. Use a parameter if this value is intended to change, or a constant otherwise.

Max Cruer
  • 100
  • 1
  • 8