Reputation: 643
I'm using the following script to divide 16 by a dynamic number of widgets within a particular sidebar:
$widgets = wp_get_sidebars_widgets();
$extra = 16/count($widgets['section-one-widgets']);
The problem is that although this does exactly what I am after, I am receiving the following error message:
PHP Warning: Division by zero in /wp-content/themes/mythemename/test.php on line 2
Is there a way to re-write the above script so that I am not receiving the error?
Thanks.
Upvotes: 0
Views: 54
Reputation: 8585
You need to check that count($widgets['section-one-widgets']);
is larger than zero first
$extra = (count($widgets['section-one-widgets']) == 0) ? 0 : 16/count($widgets['section-one-widgets']);
You should always test against the exact case that causes the failure, in this case it's not important since the count will always be zero or higher, but in another case where you are dividing by a number that is lower than zero, you want to also allow that to be processed.
Upvotes: 3
Reputation: 9034
$extra = (count($widgets['section-one-widgets']) > 0) ? 16/count($widgets['section-one-widgets']) : 0;
Upvotes: 1