RONE
RONE

Reputation: 5485

Warning: Division by zero PHP check

I am using symfony 1.1 to read an API and store the values in DB

if(($xmlArray->{'MDD'} == 0) || ($xmlArray->{'MDD'} == '0') ){  
       $autoStraObj->setRisk(' - ');
    }else{
       $autoStraObj->setRisk(round(($xmlArray->{'Pips'} / $xmlArray->{'MDD'}), 10));
    }

For Some Records, the above code results with

Warning: Division by zero in.... Not sure, that is the issue here

Upvotes: 0

Views: 1079

Answers (3)

Parixit
Parixit

Reputation: 3855

Please check for


if(empty($xmlArray->{'MDD'})){  
       $autoStraObj->setRisk(' - ');
    }else{
       $autoStraObj->setRisk(round(($xmlArray->{'Pips'} / $xmlArray->{'MDD'}), 10));
    }

Upvotes: 0

GautamD31
GautamD31

Reputation: 28763

First check $xmlArray->{'MDD'} is zero or not.If it is zero for some records then it will give you this error.

if(($xmlArray->{'MDD'} != 0) && ($xmlArray->{'MDD'} != ''))
{
   $autoStraObj->setRisk(round(($xmlArray->{'Pips'} / $xmlArray->{'MDD'}), 10));
}

Or simply

if(!empty( $xmlArray->{'MDD'}) ) {
   $autoStraObj->setRisk(round(($xmlArray->{'Pips'} / $xmlArray->{'MDD'}), 10));
}

Or as cHao said try like

if (+$xmlArray->{'MDD'} != 0) {
   $autoStraObj->setRisk(round(($xmlArray->{'Pips'} / $xmlArray->{'MDD'}), 10));
}

Upvotes: 1

Paul Denisevich
Paul Denisevich

Reputation: 2414

Because for some records $xmlArray->{'MDD'} equals zero.

To avoid this - first check if there is a 0 and do not divide, just show "0" instead.

Upvotes: 0

Related Questions