Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-未定义的变量,尽管该变量是全局定义的_Php_Laravel_Variables - Fatal编程技术网

PHP-未定义的变量,尽管该变量是全局定义的

PHP-未定义的变量,尽管该变量是全局定义的,php,laravel,variables,Php,Laravel,Variables,为什么这段代码给出了一个未定义的变量:$category\u id // declare a variable and an array $category_id; $posts = []; // get four categories $categories = Category::take(4)->get(); foreach($categories as $category){ //foreach of the four categories, get the categor

为什么这段代码给出了一个未定义的变量:$category\u id

// declare a variable and an array
$category_id;
$posts = [];

// get four categories
$categories = Category::take(4)->get();

foreach($categories as $category){
  //foreach of the four categories, get the category id
  $category_id = $category->$category_id;

  //add three posts of each of those categories to the $posts array
  $posts[] = Category::where('category_id', $category_id)->take(3)->get();

}
尽管我将顶部的变量定义为
$category\u id。为什么错误持续存在

这与
$category->$category\u id
扭曲代码有关吗?

我从来没有声明过一个变量,但没有像上面那样的内容

那么:

$category_id = null;

我从来没有声明过一个变量,没有像上面那样的东西

那么:

$category_id = null;
很可能是错的,你可能是说

$category_id = $category->category_id;
您应该(同样:最有可能)通过指定文字值或常量或类似值来初始化变量,例如

// declare a variable and an array
$category_id=null;
(编辑:或完全忽略该初始化。在您发布的代码段中,$category_id仅在循环中已为其分配了一个值,并且看起来您没有保留该变量以便在foreach循环后使用时使用。)

很可能是错的,你可能是说

$category_id = $category->category_id;
您应该(同样:最有可能)通过指定文字值或常量或类似值来初始化变量,例如

// declare a variable and an array
$category_id=null;
(编辑:或完全不使用该初始化。在您发布的代码段中,$category\u id仅在循环中被分配了一个值并且看起来您没有保留该变量以便在foreach循环之后使用时使用。)


小时:

分钟:

第二:

月份:

日期:

年份:

日期:

请解决

小时:

分钟:

第二:

月份:

日期:

年份:

日期:

请解决
是$类别id基本上为空。。没有值。$category->$category\u id;有一个值,但错误在“$category\u id=“s$category\u id”上。为什么?在哪一行出现上述错误!!,代码$category\u id=$category->$category\u id中的情况似乎很好;这条线!!是的$类别id基本上为空。。没有值。$category->$category\u id;有一个值,但错误在“$category\u id=“s$category\u id”上。为什么?在哪一行出现上述错误!!,代码$category\u id=$category->$category\u id中的情况似乎很好;这条线!!我想解释一下为什么会发生这种情况。我不确定仅仅通过写变量名来声明变量是否合法,其他什么都不做?“$category_id=null;”-但是,再说一遍,
$category->{null}
会是什么?现在我看到了问题所在。您需要为第一个id设置初始值:$category\u id=$category->$category\u id;为什么第二个变量有$,它是一个输入错误吗?我想解释一下为什么会发生这种情况。我不确定只写变量名而不写它是否合法?“$category_id=null;”-但是,再说一遍,
$category->{null}
会是什么?现在我看到问题了。您需要为第一个id设置初始值:$category\u id=$category->$category\u id;为什么第二个有$,是打字错误吗?