Javascript PHP:return和echo之间的区别
我想知道使用Javascript PHP:return和echo之间的区别,javascript,php,laravel,Javascript,Php,Laravel,我想知道使用return或echo时PHP的区别,因为在Laravel论坛上阅读的内容往往与其他问题的观点相矛盾,例如: 如果我有这样的东西: /** * Generate an appropriate response given the details that were entered by the client * * @param array $intent * @return void */ public function generatePaymentResponse(
return
或echo
时PHP的区别,因为在Laravel论坛上阅读的内容往往与其他问题的观点相矛盾,例如:
如果我有这样的东西:
/**
* Generate an appropriate response given the details that were entered by the client
*
* @param array $intent
* @return void
*/
public function generatePaymentResponse(array $intent)
{
# Note that if your API version is before 2019-02-11, 'requires_action'
# appears as 'requires_source_action'.
if (
$intent['status'] == 'requires_source_action' &&
$intent['next_action']['type'] == 'use_stripe_sdk'
) {
# Tell the client to handle the action
echo json_encode([
'requires_action' => true,
'payment_intent_client_secret' => $intent['client_secret']
]);
} else if ($intent['status'] == 'succeeded') {
# The payment didn’t need any additional actions and completed!
# Handle post-payment fulfillment
echo json_encode([
"success" => true
]);
} else {
http_response_code(500);
echo json_encode(['error' => 'Invalid PaymentIntent status']);
}
}
为什么这不是等价的
/**
* Generate an appropriate response given the details that were entered by the client
*
* @param array $intent
* @return void
*/
public function generatePaymentResponse(array $intent)
{
# Note that if your API version is before 2019-02-11, 'requires_action'
# appears as 'requires_source_action'.
if (
$intent['status'] == 'requires_source_action' &&
$intent['next_action']['type'] == 'use_stripe_sdk'
) {
# Tell the client to handle the action
return response()->json([
'requires_action' => true,
'payment_intent_client_secret' => $intent['client_secret']
], 200);
} else if ($intent['status'] == 'succeeded') {
# The payment didn’t need any additional actions and completed!
# Handle post-payment fulfillment
return response()->json([
'success' => true
], 200);
} else {
return response()->json([
'error' => 'Invalid PaymentIntent status'
], 500);
}
}
前端的脚本如下所示:
<script src="https://js.stripe.com/v3/"></script>
<script>
// Create a new Stripe client
var stripe = Stripe('{{ config('services.stripe.key') }}');
// Create a new instance of Elements
var elements = stripe.elements();
// Create a card element and bind it to a <div>
var cardElement = elements.create('card');
cardElement.mount('#card-element');
// Store elements needed as variables
var cardButton = document.getElementById('card-button');
// Bind a click event to the card button
cardButton.addEventListener('click', function(ev) {
stripe.createPaymentMethod('card', cardElement)
.then(function(result) {
if (result.error) {
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// Otherwise send paymentMethod.id to your server (see Step 2)
fetch('/confirm_payment', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
body: JSON.stringify({ payment_method_id: result.paymentMethod.id })
}).then(function(result) {
// Handle server response (see Step 3)
result.json().then(function(json) {
handleServerResponse(json);
})
});
}
});
});
function handleServerResponse(response) {
if (response.error) {
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else if (response.requires_action) {
// Use Stripe.js to handle required card action
stripe.handleCardAction(
response.payment_intent_client_secret
).then(function(result) {
if (result.error) {
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// The card action has been handled
// The PaymentIntent can be confirmed again on the server
fetch('/confirm_payment', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
body: JSON.stringify({ payment_intent_id: result.paymentIntent.id })
}).then(function(confirmResult) {
console.log(confirmResult);
return confirmResult.json();
}).then(handleServerResponse);
}
});
} else {
var errorElement = document.getElementById('card-errors');
console.log(response);
}
}
</script>
//创建一个新的条带客户端
var stripe=stripe({{config('services.stripe.key')}});
//创建元素的新实例
var elements=stripe.elements();
//创建卡片元素并将其绑定到
var cardElement=elements.create('card');
挂载(“#卡元素”);
//将所需元素存储为变量
var cardButton=document.getElementById('card-button');
//将单击事件绑定到卡按钮
cardButton.addEventListener('click',函数(ev){
stripe.createPaymentMethod('card',cardElement)
.然后(函数(结果){
if(result.error){
var errorElement=document.getElementById('card-errors');
errorElement.textContent=result.error.message;
}否则{
//否则,将paymentMethod.id发送到服务器(请参阅步骤2)
获取(“/确认付款”{
方法:“POST”,
标题:{
“内容类型”:“应用程序/json”,
'X-CSRF-TOKEN':$('meta[name=“CSRF-TOKEN”]).attr('content'))
},
正文:JSON.stringify({payment\u method\u id:result.paymentMethod.id})
}).然后(函数(结果){
//处理服务器响应(请参阅步骤3)
result.json().then(函数(json){
handleServerResponse(json);
})
});
}
});
});
函数handleServerResponse(响应){
if(response.error){
var errorElement=document.getElementById('card-errors');
errorElement.textContent=result.error.message;
}else if(响应要求采取行动){
//使用Stripe.js处理所需的卡操作
条纹(
答复.付款\意向\客户\保密
).然后(函数(结果){
if(result.error){
var errorElement=document.getElementById('card-errors');
errorElement.textContent=result.error.message;
}否则{
//卡片操作已被处理
//可以在服务器上再次确认PaymentIntent
获取(“/确认付款”{
方法:“POST”,
标题:{
“内容类型”:“应用程序/json”,
'X-CSRF-TOKEN':$('meta[name=“CSRF-TOKEN”]).attr('content'))
},
正文:JSON.stringify({payment\u intent\u id:result.paymentIntent.id})
}).then(函数(confirmResult){
console.log(confirmResult);
返回confirmResult.json();
})。然后(handleServerResponse);
}
});
}否则{
var errorElement=document.getElementById('card-errors');
控制台日志(响应);
}
}
为什么当我使用return
时,控制台只返回echo
而不返回任何内容
当我以前使用axios库时,服务器函数总是显式返回JSON,而不是回显JSON。简而言之,这取决于所讨论的函数的调用方式。如果函数使用了
echo
,则不需要generatePaymentResponse(…)
-但是如果函数使用return
,您就可以使用echo generatePaymentResponse(…)代码>。这就是为什么正确命名函数非常重要的原因。应始终相应地命名回显某个内容的函数,如echoPaymentResponse
。返回内容的函数应命名为paymentResponse
,因此用法将是echo paymentResponse(…)代码>。这是php语言基础。你可以在这里查看文档:这里是一些php代码示例:简而言之,这取决于所讨论的函数的调用方式。如果函数使用了echo
,则不需要generatePaymentResponse(…)
-但是如果函数使用return
,您就可以使用echo generatePaymentResponse(…)代码>。这就是为什么正确命名函数非常重要的原因。应始终相应地命名回显某个内容的函数,如echoPaymentResponse
。返回内容的函数应命名为paymentResponse
,因此用法将是echo paymentResponse(…)代码>。这是php语言基础。您可以在此处查看文档:此处:以下是一些php代码示例: