-2
我知道全局变量不建议使用,但在我的情况下,我没有另一种选择。我想将一些数据存储在全局变量中,以便以后在同一个脚本中使用。这里是我的代码:PHP全局变量
<?php
if(isset($_POST['details_submit'])){
$body = json_decode(file_get_contents('php://input'),true);
$GLOBALS['details'] = "";
foreach ($body as $key => $value) {
$details.= "Item : ".$value['item_name'];
$details.= ", Quantity : ".$value['quantity'];
$details.= ", Amount : ".$value['amount'];
$details.= ", Total : ".$value['total']."\n";
}
echo $details;
$GLOBALS['subtotal'] = 0;
foreach ($body as $key => $value) {
$subtotal = $value['total'] + $subtotal;
}
echo $subtotal;
}// end if statement
if(isset($_POST['customer_submit'])){
$customer = "";
$customer.= "Customer : ".$_POST['Name']."\n";
$customer.= "Email : ".$_POST['Email']."\n";
$customer.= "Phone Number : ".$_POST['Phone']."\n";
$customer.= "Residence : ".$_POST['Area'];
echo $customer;
$email = $_POST['Email'];
$to = '[email protected]';
$subject = 'Natures Touch Order';
$message = "<b>Customer Order</b> \n"
.$details."\n
<b>Customer Details</b> \n"
.$customer."\n
The subtotal is KSH ".$subtotal.".";
$headers = 'From: '.$email."\r\n";
'X-Mailer: PHP/' . phpversion();
$send1 = mail($to, $subject, $message, $headers);
}
?>
全局变量details
和subtotal
如在我的代码。我使用全局变量的原因是因为我有两个不同的if
语句,并希望在第二个if
语句中的第一个if
语句中使用某些结果。一切运作良好,但发送邮件时,我得到Undefined variable details and subtotal
错误。难道数据在第一个if语句运行时没有存储在变量中?或者可能是什么问题?
在php中的变量不是作用域的范围。 –
备注'$ headers'需要连接,而不是分号。 “X-Mailer:PHP /”。 phpversion()并不包含在你的头文件中。 – Rasclatt