Home > Blockchain >  Inline PHP code for math calculation before import with WP ALL Import
Inline PHP code for math calculation before import with WP ALL Import

Time:11-24

Currently, I am facing a problem with import where I need to write a custom inline PHP code for this. I have many numbers, for example, 0.001, 0.104, 0.302 I would like to import only this

  • if number is 0 to 0.100 - import "good"
  • if is 0.101 to 0.200 - write "medium"
  • if is 0.201 and more - write "bad"

I already use this PHP code for calculating the number written above.

function my_math($param1,$param2){
    return number_format($param1/$param2,3);}

thanks a lot for the help

CodePudding user response:

// This function uses guard clause to return a string
function my_math($param1, $param2) {
    
    // Set number variable to check if statements on
    $num = number_format($param1 / $param2, 3);

    // Return "bad" if above .2
    if ($num > .2)
        return "bad";

    // Return "good" if below .1    
    if ($num < .1)
        return "good";

    // Default return if none of the above 
    return "medium";
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Edit: added documentation and extra snippet


As asked here's an example without the $num variable

function my_math($input) {

    if ($input > .2)
        return "bad";

    if ($input < .1)
        return "good";

    return "medium";
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related