未定义的变量Array Slim Application

时间:2014-10-16 23:29:22

标签: php arrays

我收到此错误,在我的一台服务器上使用php 5.4运行正常我将代码转移到一台新的服务器,使用php 5.5.9,现在我收到此错误:

详细

类型:ErrorException 代码:8 消息:未定义的变量:propertylist 文件:/var/www/subdomains/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);
});

2 个答案:

答案 0 :(得分:1)

使用前需要创建/定义变量:

$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);
});

答案 1 :(得分:0)

我认为那个版本的PHP只是改变了变量的范围,现在它更像你期望的其他语言。如果在块中定义/创建变量,则在外部作用域中不可见。

在您的情况下,$propertylist直接在foreach块范围内定义,因此对其余代码不可见,从而导致错误。

正如@Guilherme Nascimento建议的那样,你必须在循环之外定义它 这就是说,在PHP中使用变量而不实例化它是完全正常的(但不推荐):

$var[] = 1;
// => array(1)