Search code examples
phpwordpressinlinewpallimport

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


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


Solution

  • // 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";
    }

    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";
    }