Division by zero in....on line 140

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Division by zero in....on line 140

Post by jonnyfortis »

$additional = $row_rsPayment['payment_amount_paid'] / $row_rsPayment['rental_price'];

is on that line
any ideas?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Division by zero in....on line 140

Post by Celauran »

Pretty clear. $row_rsPayment['rental_price'] is 0 or null.
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: Division by zero in....on line 140

Post by jonnyfortis »

Celauran wrote:Pretty clear. $row_rsPayment['rental_price'] is 0 or null.
so this needs to have a value other than 0 or NULL?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Division by zero in....on line 140

Post by Celauran »

Yes. You can't divide by zero.
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: Division by zero in....on line 140

Post by jonnyfortis »

Celauran wrote:Yes. You can't divide by zero.
ok i thought all that column was populated with a figure rather that 0 so i need to check

thanks
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: Division by zero in....on line 140

Post by jonnyfortis »

Celauran wrote:Yes. You can't divide by zero.
is there a way i can ignore any 0 or nulls?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Division by zero in....on line 140

Post by Celauran »

Code: Select all

$additional = (isset($row_rsPayment['rental_price']) && $row_rsPayment['rental_price'] != 0) ? $row_rsPayment['payment_amount_paid'] / $row_rsPayment['rental_price'] : 0;
That will set $additional to 0 is rental price is 0 or null, which will avoid the error. I don't know that this is necessarily the best solution, though, because I don't really have any information about what's going on beyond the one line you posted. Might make more sense to exclude such lines from the query altogether.
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: Division by zero in....on line 140

Post by jonnyfortis »

Celauran wrote:

Code: Select all

$additional = (isset($row_rsPayment['rental_price']) && $row_rsPayment['rental_price'] != 0) ? $row_rsPayment['payment_amount_paid'] / $row_rsPayment['rental_price'] : 0;
That will set $additional to 0 is rental price is 0 or null, which will avoid the error. I don't know that this is necessarily the best solution, though, because I don't really have any information about what's going on beyond the one line you posted. Might make more sense to exclude such lines from the query altogether.
code hinting is show something is incorrect
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Division by zero in....on line 140

Post by Celauran »

jonnyfortis wrote:
Celauran wrote:

Code: Select all

$additional = (isset($row_rsPayment['rental_price']) && $row_rsPayment['rental_price'] != 0) ? $row_rsPayment['payment_amount_paid'] / $row_rsPayment['rental_price'] : 0;
That will set $additional to 0 is rental price is 0 or null, which will avoid the error. I don't know that this is necessarily the best solution, though, because I don't really have any information about what's going on beyond the one line you posted. Might make more sense to exclude such lines from the query altogether.
code hinting is show something is incorrect
Mine isn't. Runs in the console, too.

Code: Select all

[08:16 am] [~]
$ php -a
Interactive shell

php > $additional = (isset($row_rsPayment['rental_price']) && $row_rsPayment['rental_price'] != 0) ? $row_rsPayment['payment_amount_paid'] / $row_rsPayment['rental_price'] : 0;
php > print_r($additional);
0
php >
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: Division by zero in....on line 140

Post by jonnyfortis »

Celauran wrote:
jonnyfortis wrote:
Celauran wrote:

Code: Select all

$additional = (isset($row_rsPayment['rental_price']) && $row_rsPayment['rental_price'] != 0) ? $row_rsPayment['payment_amount_paid'] / $row_rsPayment['rental_price'] : 0;
That will set $additional to 0 is rental price is 0 or null, which will avoid the error. I don't know that this is necessarily the best solution, though, because I don't really have any information about what's going on beyond the one line you posted. Might make more sense to exclude such lines from the query altogether.
code hinting is show something is incorrect
Mine isn't. Runs in the console, too.

Code: Select all

[08:16 am] [~]
$ php -a
Interactive shell

php > $additional = (isset($row_rsPayment['rental_price']) && $row_rsPayment['rental_price'] != 0) ? $row_rsPayment['payment_amount_paid'] / $row_rsPayment['rental_price'] : 0;
php > print_r($additional);
0
php >
i was missing a bracket.
thanks
Post Reply