3v4l.org

run code in 300+ PHP versions simultaneously
<?php class VarStorage { protected $_property; public function setProperty($value) { $this->_property = $value; } public function getProperty() { return $this->_property; } } function mem_print($message, $before, $after) { $difference = ($after - $before) / 1000000; echo sprintf($message, sprintf('%.2fM', $difference)); echo "\n"; } echo "Testing class in a PHP script\n\n"; // Create massive variable $mBeforeA = memory_get_usage(); $a1 = str_repeat('a', 3000000); $mAfterA = memory_get_usage(); mem_print('Creating $a1: %s', $mBeforeA, $mAfterA); // Refcount increases $a2 = str_repeat('a', 3000000); $mBeforeB = memory_get_usage(); $b = $a2; $mAfterB = memory_get_usage(); mem_print('$b = $a2: %s', $mBeforeB, $mAfterB); // Separation $mBeforeSeparation = memory_get_usage(); $b .= 'data'; $mAfterSeparation = memory_get_usage(); mem_print('$b .= \'data\': %s', $mBeforeSeparation, $mAfterSeparation); // Setting to a class property $a4 = str_repeat('a', 3000000); $class = new VarStorage; $mBeforeStorageSet = memory_get_usage(); $class->setProperty($a4); $mAfterStorageSet = memory_get_usage(); mem_print('$class->setProperty($a4): %s', $mBeforeStorageSet, $mAfterStorageSet); // Getting from a class property $mBeforeStorageGet = memory_get_usage(); $result = $class->getProperty(); $mAfterStorageGet = memory_get_usage(); mem_print('$result = $class->getProperty(): %s', $mBeforeStorageGet, $mAfterStorageGet); $mBeforeSeparationFromStorage = memory_get_usage(); $result .= 'data'; $mAfterSeparationFromStorage = memory_get_usage(); mem_print('$result .= \'data\': %s', $mBeforeSeparationFromStorage, $mAfterSeparationFromStorage);
Output for 5.2.1 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.7
Testing class in a PHP script Creating $a1: 3.00M $b = $a2: 0.00M $b .= 'data': 3.00M $class->setProperty($a4): 0.00M $result = $class->getProperty(): 0.00M $result .= 'data': 3.00M
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0
Testing class in a PHP script Fatal error: Call to undefined function memory_get_usage() in /in/94oXF on line 26
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/94oXF on line 4
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
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/94oXF on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/94oXF on line 4
Process exited with code 255.

preferences:
174.27 ms | 401 KiB | 215 Q