php - 从数组打印值有时不起作用

标签 php laravel laravel-5 eloquent

这太奇怪了。第一个不打印任何内容,而如果我做一个附有一些随机文本的 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了解更多信息。

当您将 intid: 连接时,字符串将传递给 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/

相关文章:

php - MySQL查询使用php基于列更新多行

php - 如何在 Laravel 5.4 中多次使用变量模型

jQuery 单击一个按钮最终单击具有相同类的所有按钮

php - 限制 Laravel 5 加密长度

php - Laravel 5 - 如何在 View 中访问存储中上传的图像?

php - 将产品缩略图添加到 Woocommerce 管理员订单列表

php - 包含来自父目录或其他目录的文件

php - 如何使用 PHP 在 MySQL 中插入 now() 函数

Laravel 5.4,什么是电子商务网站的最佳 session 驱动程序

node.js - 如何仅安装 package.json 文件中定义的那些包