这太奇怪了。第一个不打印任何内容,而如果我做一个附有一些随机文本的 die
,它会打印 id
。有人可以解释一下吗?
这是有效的:
$product_ids = ProductToOption::groupBy('product_id')->get(['product_id']);
foreach($product_ids as $product_id) {
die("id: ".$product_id->product_id);
array_push($filter_array, $product_id->product_id);
}
但这一个不是:
$product_ids = ProductToOption::groupBy('product_id')->get(['product_id']);
foreach($product_ids as $product_id) {
die($product_id->product_id);
array_push($filter_array, $product_id->product_id);
}
最佳答案
如果传递给 die() 的值是一个 int,它不会被打印出来,而是用作执行脚本的进程的返回码 - 参见 http://php.net/manual/en/function.exit.php了解更多信息。
当您将 int 与 id: 连接时,字符串将传递给 die() 而不是 integer ,这就是它导致 id: 1 被打印的原因。
来自 exit()
上的手册:
If status is a string, this function prints the status just before exiting.
If status is an integer, that value will be used as the exit status and not printed. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.
Note: PHP >= 4.2.0 does NOT print the status if it is an integer.
关于php - 从数组打印值有时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34179642/