3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Ampersand\DevTest\BasicOop; interface NumberInterface { /** * Adds n to this number * * @param int $n The amount to add to this number * @return int The new integer value of this number */ public function add($n); /** * Subtracts n from this number * * @param int $n The amount to subtract from this number * @return int The new integer value of this number */ public function subtract($n); /** * Multiplies this number by n * * @param int $n The amount to multiply this number by * @return int The new integer value of this number */ public function multiply($n); /** * Divides this number by n * * @param int $n The amount to divide this number by * @return int The new integer value of this number */ public function divide($n); /** * Returns the integer value of this number * * @return int The current integer value of this number */ public function toInt(); } class NumberTest { public function testConstruct() { $n = new Number; $this->assertEquals(0, $n->toInt()); $n = new Number(0); $this->assertEquals(0, $n->toInt()); $n = new Number(3); $this->assertEquals(3, $n->toInt()); } public function testAdd() { $n = new Number; $n->add(5); $this->assertEquals(5, $n->toInt()); $n->add(-7); $this->assertEquals(-2, $n->toInt()); } public function testSubtract() { $n = new Number; $n->subtract(8); $this->assertEquals(-8, $n->toInt()); $n->subtract(-10); $this->assertEquals(2, $n->toInt()); } public function testMultiply() { $n = new Number(1); $n->multiply(10); $this->assertEquals(10, $n->toInt()); $n->multiply(6); $this->assertEquals(60, $n->toInt()); } public function testDivide() { $n = new Number(100); $n->divide(5); $this->assertEquals(20, $n->toInt()); $n->divide(5); $this->assertEquals(4, $n->toInt()); $hasThrownEx = false; try { $n->divide(0); } catch (\InvalidArgumentException $e) { $hasThrownEx = true; } $this->assertEquals(true, $hasThrownEx); } private function assertEquals($expected, $actual) { if ($expect !== $actual) { echo "\n"; var_dump($actual); echo ' does not match expected '; var_dump($expected); echo "\n"; } } } class Number implements NumberInterface { private $value; public function __construct($value = 0) { $this->value = $value; } public function add($value) { $this->value += $value; return $this->value; } public function subtract($value) { $this->value -= $value; return $this->value; } public function multiply($value) { $this->value *= $value; return $this->value; } public function divide($value) { if (0 === $value) { throw new \InvalidArgumentException; } $this->value /= $value; return $this->value; } public function toInt() { return $this->value; } } $test = new NumberTest; $test->testConstruct(); $test->testAdd(); $test->testSubtract(); $test->testMultiply(); $test->testDivide();
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.7
Warning: Undefined variable $expect in /in/ccKr7 on line 115 int(0) does not match expected int(0) Warning: Undefined variable $expect in /in/ccKr7 on line 115 int(0) does not match expected int(0) Warning: Undefined variable $expect in /in/ccKr7 on line 115 int(3) does not match expected int(3) Warning: Undefined variable $expect in /in/ccKr7 on line 115 int(5) does not match expected int(5) Warning: Undefined variable $expect in /in/ccKr7 on line 115 int(-2) does not match expected int(-2) Warning: Undefined variable $expect in /in/ccKr7 on line 115 int(-8) does not match expected int(-8) Warning: Undefined variable $expect in /in/ccKr7 on line 115 int(2) does not match expected int(2) Warning: Undefined variable $expect in /in/ccKr7 on line 115 int(10) does not match expected int(10) Warning: Undefined variable $expect in /in/ccKr7 on line 115 int(60) does not match expected int(60) Warning: Undefined variable $expect in /in/ccKr7 on line 115 int(20) does not match expected int(20) Warning: Undefined variable $expect in /in/ccKr7 on line 115 int(4) does not match expected int(4) Warning: Undefined variable $expect in /in/ccKr7 on line 115 bool(true) does not match expected bool(true)
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined variable: expect in /in/ccKr7 on line 115 int(0) does not match expected int(0) Notice: Undefined variable: expect in /in/ccKr7 on line 115 int(0) does not match expected int(0) Notice: Undefined variable: expect in /in/ccKr7 on line 115 int(3) does not match expected int(3) Notice: Undefined variable: expect in /in/ccKr7 on line 115 int(5) does not match expected int(5) Notice: Undefined variable: expect in /in/ccKr7 on line 115 int(-2) does not match expected int(-2) Notice: Undefined variable: expect in /in/ccKr7 on line 115 int(-8) does not match expected int(-8) Notice: Undefined variable: expect in /in/ccKr7 on line 115 int(2) does not match expected int(2) Notice: Undefined variable: expect in /in/ccKr7 on line 115 int(10) does not match expected int(10) Notice: Undefined variable: expect in /in/ccKr7 on line 115 int(60) does not match expected int(60) Notice: Undefined variable: expect in /in/ccKr7 on line 115 int(20) does not match expected int(20) Notice: Undefined variable: expect in /in/ccKr7 on line 115 int(4) does not match expected int(4) Notice: Undefined variable: expect in /in/ccKr7 on line 115 bool(true) does not match expected bool(true)
Output for 7.3.32 - 7.3.33
int(0) does not match expected int(0) int(0) does not match expected int(0) int(3) does not match expected int(3) int(5) does not match expected int(5) int(-2) does not match expected int(-2) int(-8) does not match expected int(-8) int(2) does not match expected int(2) int(10) does not match expected int(10) int(60) does not match expected int(60) int(20) does not match expected int(20) int(4) does not match expected int(4) bool(true) does not match expected bool(true)
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/ccKr7 on line 2
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/ccKr7 on line 2
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/ccKr7 on line 2
Process exited with code 255.

preferences:
246.77 ms | 401 KiB | 328 Q