简体   繁体   中英

PHP variable value not printing outside try catch

I am trying to print variable value outside try catch code but it is not getting printed. following is my code

 $error;
 try {

  $customer = \Stripe\Customer::create(array(
      "description"=>"Customer",
        "source" => $token,
        "email" => $email,
      "plan" => "armorax"
          )
    );

 $payment = \Stripe\Charge::create(array(
 'amount'        => $amount,
 'currency'      => 'usd',

 'description'   => $_POST['description'],
  "customer" => $customer->id

 )
 );




 } catch(\Stripe\Error\Card $e) {

   $body = $e->getJsonBody();
   $err  = $body['error'];
    $error= 'Status is:' . $e->getHttpStatus() . "\n";
 }

 //SOme HTML CODE
  <div class="alert"><?php echo $error; ?></div>

$error should print an error if there is but is not printing error.

A few things. What are you doing with the line $error; ? If you're trying to initialize the variable, just set it to null with $error = null; .

Secondly, Make sure you're writing your try/catch correctly.

try {
 //SomePhp code here
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

If it's not echoing the exception, then the try is not failing to begin with.

PHP documentation - Try/Catch

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