PHP 7.2 count()函数不起作用 [英] PHP 7.2 count() function is not working

查看:384
本文介绍了PHP 7.2 count()函数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行中的php应用程序,并且在php 7.0版本上运行良好。但是当我将php版本升级到7.2时。我收到此错误:

I have a working php application and it is running fine on php 7.0 version. But when I upgrade a php version to 7.2. I am getting this error:

count(): Parameter must be an array or an object that implements Countable

在将我的数据与count函数进行比较时,代码出现错误。例如,这是我的代码:

I am getting errors on code where I am comparing my data with count function. For example this is my code:

$keytest = KeyUser::where('key', '=', $key)->first();
 if (count($keytest) == 1) {
   //logic ... 
  }

我正在使用laravel在运行查询的地方进行计数,如果它等于1,则逻辑应该起作用。

I am using laravel where I am running a query and counting it if it is equal to 1 then logic should work.

所以我的问题是我已经在许多控制器上编写了这种逻辑,如果我必须一一更改所有内容,那可能会成为噩梦。所以有什么办法可以写一个全局函数来使计数工作,因为它在php旧版本中工作。什么是最简单的解决方法。

So my problem is I have written this kind of logic on many controllers and if I have to change everything one by one it could become nightmare. So is there any way where I can write a global function to make count work as it was working in php older version. What can be the easiest fix.

推荐答案

可以使用禁用错误处理来解决此问题。请参考此链接以获取解决方案: Laravel与php 7.2不兼容

This problem can be handle using disable error handling. Please refer this link for solution: Laravel not compatiable with php 7.2

在这里我找到了解决您的问题的方法,只需在您的控制器内部编写此代码,或者如果您想使其适用于整个应用程序,则在route.php中编写以下代码:

Here I found a solution to your problem simply write this code inside your controller or if you want to make it work for whole application write this code in route.php:

 //app/Http/routes.php

 if (version_compare(PHP_VERSION, '7.2.0', '>=')) {
// Ignores notices and reports all other kinds... and warnings
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
// error_reporting(E_ALL ^ E_WARNING); // Maybe this is enough
}

我知道这不是最好的解决方案,但是它可以是一个很好的技巧。

I know this is not the best solution but it can be a good hack.

这篇关于PHP 7.2 count()函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆