在我使用php 5.4的服务器上运行良好的错误中,我得到了这个错误,我用php 5.5.9将代码转移到一个新服务器,现在我得到了这个错误:
详细信息
类型: ErrorException代码:8消息:未定义变量: propertylist文件:/var/www/子域/api/index.php行: 57跟踪
守则:
$app->get("/propertylist/", function () use ($app, $db) {
$app->response()->header("Content-Type", "application/json");
ob_start('ob_gzhandler');
$req = $app->request();
$bed = $req->get('bed');
$bath = $req->get('bath');
$city = $req->get('city');
$zip = $req->get('zip');
if($bed ==''){$bed=0;}
if($bath ==''){$bath=0;}
if($zip ==''){
$properties = $db->rets_property_listing_mrmls_resi->limit(2500,0)->where("Bedrooms >= ?", $bed)->where("City LIKE ?", "%$city%")->where("BathsTotal >= ?", $bath);
}else{
$properties = $db->rets_property_listing_mrmls_resi->limit(2500,0)->where("Bedrooms >= ?", $bed)->where("ZipCode LIKE ?", "%$zip%")->where("BathsTotal >= ?", $bath);
}
foreach ($properties as $property) {
$propertylist[] = array(
"MLSnumber" => $property["MLnumber"],
"ListPrice" => number_format($property["ListPrice"]),
"StreetNumber" => $property["StreetNumber"],
"StreetName" => $property["StreetName"],
"SqFt" => $property["SquareFootageStructure"],
"PropertyDescription" => summaryMode($property["PropertyDescription"],15),
"Bedrooms" => $property["Bedrooms"],
"BathsTotal" => $property["BathsTotal"],
"LO_Name" => $property["LO_Name"]
);
}
echo json_encode($propertylist);
});发布于 2014-10-16 23:34:20
您需要在使用前创建/定义变量:
$app->get("/propertylist/", function () use ($app, $db) {
$app->response()->header("Content-Type", "application/json");
ob_start('ob_gzhandler');
$req = $app->request();
$bed = $req->get('bed');
$bath = $req->get('bath');
$city = $req->get('city');
$zip = $req->get('zip');
if($bed ==''){$bed=0;}
if($bath ==''){$bath=0;}
if($zip ==''){
$properties = $db->rets_property_listing_mrmls_resi->limit(2500,0)->where("Bedrooms >= ?", $bed)->where("City LIKE ?", "%$city%")->where("BathsTotal >= ?", $bath);
}else{
$properties = $db->rets_property_listing_mrmls_resi->limit(2500,0)->where("Bedrooms >= ?", $bed)->where("ZipCode LIKE ?", "%$zip%")->where("BathsTotal >= ?", $bath);
}
$propertylist = array(); //Create variable type array
foreach ($properties as $property) {
$propertylist[] = array(
"MLSnumber" => $property["MLnumber"],
"ListPrice" => number_format($property["ListPrice"]),
"StreetNumber" => $property["StreetNumber"],
"StreetName" => $property["StreetName"],
"SqFt" => $property["SquareFootageStructure"],
"PropertyDescription" => summaryMode($property["PropertyDescription"],15),
"Bedrooms" => $property["Bedrooms"],
"BathsTotal" => $property["BathsTotal"],
"LO_Name" => $property["LO_Name"]
);
}
echo json_encode($propertylist);
});发布于 2014-10-16 23:44:14
我认为PHP版本只是改变了变量的作用域,现在它更像您在其他语言中所期望的那样。如果在块中定义/创建变量,则它在外部作用域中不可见。
在您的示例中,$propertylist是直接在foreach块作用域中定义的,因此它对代码的其余部分是不可见的,从而导致错误。
正如@建议的那样,您必须在循环之外定义它。
也就是说,在PHP中使用一个变量而不实例化它是非常好的(但不建议):
$var[] = 1;
// => array(1)https://stackoverflow.com/questions/26415572
复制相似问题