PHP变量值未在try catch外部打印
我试图在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
$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来定义变量的范围,但它仍然不起作用。