Search code examples
phpmathsyntaxcalc

How to split a longer formula into multiple lines with PHP?


I have to do some more complicated formulas with PHP. It would be cool if I could split the formula into multiple lines. I know how to do that with strings, but I have no idea how to do it within one single calculation.

i want to do something like:

public function example_calc(){
  $result = $var1 * $var2 +
    + $var3 * $var4 -
    - $var5 / $var6;
  return $result;
}

Solution

  • Don't duplicate + and - signs across lines, just split it normally

    public function example_calc(){
      $result = $var1 * $var2
        + $var3 * $var4
        - $var5 / $var6;
      return $result;
    }
    

    or

    public function example_calc(){
      $result = $var1 * $var2 +
        $var3 * $var4 -
        $var5 / $var6;
      return $result;
    }
    

    and the really cool will just work

    It's the ; that marks the end of the "line" of code, not the carriage return/line feed