calc - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of calc.php

  1. <?php
  2. class MyCalculator {
  3. private $_fval, $_sval;
  4. public function __construct( $fval, $sval ) {
  5. $this->_fval = $fval;
  6. $this->_sval = $sval;
  7. }
  8. public function add() {
  9. return $this->_fval + $this->_sval;
  10. }
  11. public function subtract() {
  12. return $this->_fval - $this->_sval;
  13. }
  14. public function multiply() {
  15. return $this->_fval * $this->_sval;
  16. }
  17. public function divide() {
  18. return $this->_fval / $this->_sval;
  19. }
  20. }
  21. $mycalc = new MyCalculator(12, 6); 
  22. echo $mycalc-> add()."\n"; // Displays 18 
  23. echo $mycalc-> multiply()."\n"; // Displays 72
  24. echo $mycalc-> subtract()."\n"; // Displays 6
  25. echo $mycalc-> divide()."\n"; // Displays 2
  26. ?>
File Description
  • calc
  • PHP Code
  • 17 Feb-2021
  • 649 Bytes
You can Share it: