mardi 14 février 2017

PHP type conversion in division

This seems pretty trivial, but I am not able to figure out what is wrong with this one.

I have some data from database in PHP object, on which I am trying to perform division operation. This is the culprit code -

$machineSkill->machine_exp = ($machineSkill->machine_exp)/12;

For some record in DB, the value of machine_exp is coming as "48". It is stored as textfield. You can see in snapshot below, that if I comment above code, this is what is coming from DB. enter image description here

Now when I uncomment this line, this is the snapshot of what is happening -

enter image description here

The value I was expecting was 4, but it is coming to be a decimal value.

When I am typecasting it -

$machineSkill->machine_exp = ((int)$machineSkill->machine_exp)/12;

Or,

$machineSkill->machine_exp = (intval($machineSkill->machine_exp))/12;

The value of the variable becomes 0. What am I doing wrong, and how do I solve this?

Aucun commentaire:

Enregistrer un commentaire