id,summary,reporter,owner,description,type,status,priority,milestone,component,version,severity,resolution,keywords,cc,focuses 43583,Introduce new PHP cross-version compat function `is_countable()`,jrf,SergeyBiryukov,"PHP 7.2 introduced a warning when `count()` is used on something non-countable. PHP 7.3 will introduce a new function called `is_countable()` which will basically check: {{{#!php if (is_array($foo) || $foo instanceof Countable) { // $foo is countable } }}} See: https://wiki.php.net/rfc/is-countable Note: the RFC has passed the vote and the function has already been implemented and merged into PHP Core. I would like to suggest adding this new function to the `wp-includes/compat.php` file, like so: {{{#!php if ( ! function_exists( 'is_countable' ) ) : function is_countable( $var ) { return ( is_array( $var ) || $var instanceof Countable ); } } endif; }}} This seems like low hanging fruit. Both the [http://php.net/manual/en/language.operators.type.php `instanceof` operator] as well as the [http://php.net/manual/class.countable.php `Countable` interface] were already available in PHP 5.2.4, so there should be no cross-version compatibility issues with introducing this function. The function would provide an easy helper function to help fix the various issues where the warning is currently being reported - both for Core as well as for plugin/theme devs -. Existing code like this: {{{#!php if ( count( $var ) > 0 ) { // Do something. } }}} can then be replaced by: {{{#!php if ( is_countable( $var ) && count( $var ) > 0 ) { // Do something. } }}} This would prevent new bugs from being introduced by people ""fixing"" these issues using an extra check with `empty()` which I've seen being suggested a couple of times now and which really is not a good idea as `empty()` will also return `false` when a non-zero integer, a boolean `true` or a non-zero string is passed. All of which would still result in the PHP 7.2 warning being thrown. The function would also still allow for code refactoring from arrays to objects. /cc @schlessera I would suggest introducing this function ASAP and not to wait for a major release. Related issues (there are probably more, but a quick search yielded these): #43374, #43368, #43312, #43216, #43201, #43216, #43201, #42860, #42814, #42147, #42498 ",enhancement,closed,normal,4.9.6,General,,normal,fixed,php73 has-patch has-unit-tests has-dev-note,,