Question

I was receiving a PHP warning for 'Division By Zero' due to one of the equations on my site:

$VidListAppRate = (($VLA['Likes'] / $VLA['views']) * 100;

After I realized I did not account for the fact that the variable $VLA['views'] could be zero, I changed this code to the following, thinking it would eradicate the warning:

$VidListAppRate = ($VLA['views'] === 0) ? 0 : ($VLA['Likes'] / $VLA['views']) * 100;

However, the warning still appeared (note: $VLA['views'] is an int) . I tried replacing 0 with a string:

$VidListAppRate = ($VLA['views'] === 0) ? 'N/A' : ($VLA['Likes'] / $VLA['views']) * 100;

but still I receive the warning. I know notices, warnings, and error messages in my php error_log are my friends, but how would I rewrite my code to appease this warning?

Was it helpful?

Solution

An exact match is expected here:

$VidListAppRate = ($VLA['views'] === 0)...

Maybe you need:

$VidListAppRate = ($VLA['views'] == 0)...

Or:

$VidListAppRate = ($VLA['views'] === '0')... 

Or:

$VidListAppRate = (!$VLA['views'])...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top