Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP变量值未在try catch外部打印_Php_Exception - Fatal编程技术网

PHP变量值未在try catch外部打印

PHP变量值未在try catch外部打印,php,exception,Php,Exception,我试图在try-catch代码外打印变量值,但无法打印。 下面是我的代码 $error; try { $customer = \Stripe\Customer::create(array( "description"=>"Customer", "source" => $token, "email" => $email, "plan" => "armorax" ) ); $pa

我试图在try-catch代码外打印变量值,但无法打印。 下面是我的代码

 $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;
试一试{
$customer=\Stripe\customer::创建(数组)(
“说明”=>“客户”,
“source”=>$token,
“email”=>$email,
“计划”=>“armorax”
)
);
$payment=\Stripe\Charge::创建(数组)(
“金额”=>美元金额,
“货币”=>“美元”,
'description'=>$\u POST['description'],
“客户”=>$customer->id
)
);
}捕获(\Stripe\Error\Card$e){
$body=$e->getJsonBody();
$err=$body['error'];
$error='状态为:'.$e->getHttpStatus()。“\n”;
}
//一些HTML代码

$error应该打印一个错误,如果有但没有打印错误。

一些事情。您正在使用行
$error?如果您试图初始化变量,只需使用
$error=null将其设置为null

其次,确保您正确地编写了try/catch

try {
 //SomePhp code here
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
如果它没有响应异常,那么尝试并没有失败


1<代码>$error该怎么办?这只是一个NOP2。您在哪里为
$error
赋值?3. <代码>例外示例
缺少美元符号!4.您的php结束标记在哪里?在
try
中是否有错误会在
catch
中被捕获?如果发生异常,您只需输入
catch
部分。问题更新检查,请立即查看。当我提到它是在php中时,我已经更新了我的问题,请检查并立即回复谢谢。您是否尝试过通过
echo()
var_dump()
进行调试?你确定你正在输入代码的
捕获部分吗?是的,朋友,我用print\r()输入了,但没有成功。我还尝试在起始行添加global$error来定义变量的范围,但它仍然不起作用。