未定义的变量变量声明

问题描述:

出于某种原因,我得到未定义的变量和字符串数组转换 我不明白,为什么无论这些正在发生的事情未定义的变量变量声明

Notice: Undefined variable: body in C:\Users\New\Desktop\xampp\htdocs\yakov\sendemail.php</b> on line <b>20 

Notice: Array to string conversion in C:\Users\New\Desktop\xampp\htdocs\yakov\sendemail.php on line 33 
"Array'services.html: ''services.html'\n\n'new york: ''new york'\n\n'new york: ''new york'\n\n'round_trip: ''round_trip'\n\n'2016-09-16: ''2016-09-16'\n\n'2016-09-23: ''2016-09-23'\n\n'nonstop: ''nonstop'\n\n'flexible: ''flexible'\n\n'Business: ''Business'\n\n'1 Adult: ''1 Adult'\n\n'some: ''some'\n\n'one: ''one'\n\n'[email protected]: ''[email protected]'\n\n'new york: ''new york'\n\n'dsfa\n: ''dsfa\n'\n\n'4127117117: ''4127117117'\n\n'me: ''me'\n\n;" 

这里是我的代码这是导致我试图玩弄它的问题

<?php 
    header('Content-type: application/json'); 
    $status = array(
     'type'=>'success', 
     'message'=>'Thank you for contacting us. We will contact you as early as possible.' 
    ); 
    //print phpinfo(); 
    error_reporting(-1); 
ini_set('display_errors', 'On'); 
//set_error_handler("var_dump"); 
$body; 
$email; 
$subject; 
$email_from; 
$email_to = '[email protected]'; 
if (!empty($_REQUEST)) { 
    $body; 
    foreach($_REQUEST as $key => $val) { 
     if (isset($_REQUEST[$key])) { 
      $body .= "'". $_REQUEST[$key] .": '" . $val . "\n\n"; 
     } 

    } 
$email = isset($_REQUEST['email']) ? trim(stripslashes($_REQUEST['email'])) : "NA"; 
$subject = isset($_REQUEST['subject']) ? trim(stripslashes($_REQUEST['subject'])) : "NA"; 
    $body .= ";"; 
    $email_from = $email; 
    //$email_to = '[email protected]';// your email 
    $body; 
} 
$success = mail($email_to, $subject, $body, 'From: <'.$email_from.'>'); 

    echo json_encode($status .$body); 
//} 
    die; 

任何其他建议,将不胜感激,因为我是新来的后端

+1

首先:你正在做的只是$ body;很多。首先将它定义为一个字符串,不要重新定义它($ body ='';) – user3791775

我不知道你这个做什么:

$body; 

我在你的代码看到三个流浪$body;的。让那些人离开那里。正如在评论中提到的,只是把它定义为一次在顶部的字符串:

$body = ""; 

然后你就可以连接其他字符串给它的所有你的愿望。

数组字符串错误可能是由于这样的事实,你试图连接一个字符串数组:

echo json_encode($status .$body); 
// ^-- this won't work. $status is an array. $body is a string. 

如果你只是呼应的是JSON的乐趣,你总是可以首先将你的身体字符串添加到状态数组中,然后将其回显出来:

$status['body'] = $body; 
echo json_encode($status);