0

For example, suppose i have this very simple function

    $a = 1;
    $b = 2;

    function Sum()
{
    global $a, $b;

    $b = $a + $b;
}

    Sum();
    echo $b;

This normally would output 3 directly. But is it possible to make php show the operations being done, like 3 = 1 + 2? (similar to a console.log) Thanks!

Mikael Blomkvist
  • 149
  • 2
  • 13

1 Answers1

0

You can customize the function a little bit to achieve this:

function Sum($a, $b)
{
    $c = $a + $b;
    return "{$c} = {$a} + {$b}";
}

$a = 1;
$b = 2;
echo sum($a, $b);

Note that I've changed the function to use parameters. Using globals is not a very good idea. See this answer to know why.

The above code outputs:

3 = 1 + 2

If you want your function to perform multiple operations, you can create a switch block and define the operations, and display the corresponding symbol (+, -, x, ÷ etc.)

Community
  • 1
  • 1
Amal Murali
  • 75,622
  • 18
  • 128
  • 150