PHP 警告:除以零错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5007429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Warning: Division by zero error
提问by acctman
I getting this error and not sure how to fix it. Line 162 is the first line in the php code. I looked at some of the related questions and all my quote marks seem to be in the right place
我收到此错误,但不知道如何解决。第 162 行是 php 代码的第一行。我查看了一些相关问题,我所有的引号似乎都在正确的位置
Per page:
<input type=text name=per_page value="<?php echo $en['per_page']; ?>" size=6>
Go to page:
<select name=page size=1>
<?php
$pages = ceil(sql_num_rows($result)/$en['per_page']);
for ($k=1;$k<=$pages;$k++)
echo '<option value='.$k.($en[page] == $k ? ' selected':'').'>'.
$k.'</option>';
echo '</select>';
if ($en[page]<$pages)
echo '<input type=submit name=next value="Next page">';
?>
回答by Rudu
What's the value of $en['per_page']
(I'm guessing zero)
的价值是多少$en['per_page']
(我猜是零)
回答by simshaun
Try to echo $en['per_page']
and make sure it's got the correct value. (I bet it doesnt).
尝试echo $en['per_page']
并确保它具有正确的值。(我打赌它没有)。
Unrelated: $en[page]
Should probably be $en['page']
(assuming page is really not a constant)
无关:$en[page]
应该是$en['page']
(假设页面真的不是一个常数)
回答by slifty
Any time you divide by a variable, if the variable is 0 you will get that error.
任何时候你除以一个变量,如果变量为 0,你就会得到那个错误。
The way to prevent it would be to check to ensure that $en['per_page'] is not zero, since that appears to be your only division and is probably where your problem is.
防止它的方法是检查以确保 $en['per_page'] 不为零,因为这似乎是您唯一的部门,并且可能是您的问题所在。
回答by John Cartwright
Because you cannot have a denominator value of 0.
因为分母值不能为 0。
Check the value of $en['per_page'] > 0 prior to applying it to a division.
在将 $en['per_page'] > 0 应用于分区之前,请检查它的值。
回答by Rocket Hazmat
If you're getting a divide by zero error, then $en['per_page']
may not be correct. It's probably a zero value.
如果您得到除以零错误,则$en['per_page']
可能不正确。它可能是一个零值。
Also, all your HTML attributes need to be in quotes.
For example:
此外,您的所有 HTML 属性都需要用引号引起来。例如:
<input type=text name=per_page
Should be:
<input type="text" name="per_page"
<input type=text name=per_page
应该:
<input type="text" name="per_page"
Your array indexes should be quoted too.
你的数组索引也应该被引用。
$en[page]
Should be:
应该:
$en['page']
回答by Ashfaq Ahmed
Here is the solution:
这是解决方案:
Per page:
每页:
<input type=text name=per_page value="<?php echo $en['per_page']; ?>" size=6>
Go to page:
转到页面:
<?php
$pages = @ceil(sql_num_rows($result)/$en['per_page']);
for ($k=1;$k<=$pages;$k++)
echo '<option value='.$k.($en[page] == $k ? ' selected':'').'>'.
$k.'</option>';
echo '</select>';
if ($en[page]<$pages)
echo '<input type=submit name=next value="Next page">';
?>
Just used @ before ceil / division.
只是在 ceil / Division 之前使用了 @。