Skip to content

Instantly share code, notes, and snippets.

View ameenross's full-sized avatar

Ameen Ross ameenross

View GitHub Profile
@ameenross
ameenross / suppress-warning-from-count-on-non-countable.php
Last active December 20, 2018 13:48
PHP 7.2 suppress warning from count
<?php
/**
* This is an alternative way of handling warnings from calling count() on
* non-countables. It's just an error handler that drops the warning, and calls
* the previous error handler for everything else (if there is one).
*/
$previous = set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) use (&$previous) {
// Do nothing with an E_WARNING from count.
<?php
/**
* Some PHP frameworks are sensitive to non-fatal PHP errors (even notices).
* Those same frameworks are strict about other things, like typing, and only
* use strict comparisons. On the other hand, they use count() on scalars
* without doing a type check, and rely on the bogus return value of `1`.
* The PHP devs considered this legacy behavior of PHP's count (to return `1`
* for scalars) a problem, and relying on it a BUG. So in PHP 7.2, the function
* still returns the same values, but it now triggers an E_WARNING.
* Since those frameworks use count on scalars in various places, now PHP