3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ToString { /** * This function will convert any variable to a string representation of that variable * * @param array $var The variable to print * @param int $max_lines Maximum number of lines that can be used * @param int $max_depth Maximum depth to print * @param int $min_depth Minimum depth to print * @return string String representation of Variable */ public static function variable( $var, $max_lines = 40, $max_depth = 6, $min_depth = 2 ) { $result = ToString::_varToString( $var, $max_lines, $max_depth, $min_depth ); return $result['text']; } /** * Recursive function to print out variables much like print_r * * @param array $var The variable to print * @param int $max_lines Maximum number of lines that can be used * @param int $max_depth Maximum depth to print * @param int $min_depth Minimum depth to print * @param int $depth Current Depth * @param int $lines Current Lines Used * @param int $lines_reserved Lines reserved for previous array depth * @param int $indent How much indentation to start on * @return string String representation of Variable */ protected static function _varToString( $var, $max_lines, $max_depth, $min_depth, $depth = 0, $lines = 1, $lines_reserved = 0, $indent = 0 ) { if ( is_string( $var ) ) { $return = "\"" . $var . "\""; } elseif ( is_array( $var ) ) { $return = ToString::print_array( $var, $max_lines, $max_depth, $min_depth, $depth, $lines, $lines_reserved, $indent ); } elseif ( is_null( $var ) ) { $return = 'NULL'; } elseif ( is_bool( $var ) ) { $return = ( $var ) ? 'true' : 'false'; } elseif ( is_object( $var ) ) { $return = 'Object( '. get_class( $var ) .' )'; } elseif ( is_resource( $var ) ) { $return = 'Resource( '. get_resource_type( $var ) .' )'; } else { $return = $var; } if ( is_array( $return ) ) { return $return; } else { return array( 'text' => $return, 'lines' => 1 ); } } /** * Recursive function to print out arrays much like print_r * * @param array $array The array to print * @param int $max_lines Maximum number of lines that can be used * @param int $max_depth Maximum depth to print * @param int $min_depth Minimum depth to print * @param int $depth Current Depth * @param int $lines Current Lines Used * @param int $lines_reserved Lines reserved for previous array depth * @param int $indent How much indentation to start on * @return string String representation of Array */ protected static function print_array( $array, $max_lines, $max_depth, $min_depth, $depth, $lines, $lines_reserved, $indent ) { $start_lines = $lines; $lines++; $count = count( $array ); if ( ! empty( $array ) && $depth < $max_depth && ( ( $depth <= $min_depth && $lines + $depth + 2 < $max_lines ) || ( $depth > $min_depth && ( $lines + $lines_reserved - 1 ) < $max_lines ) ) ) { $lines += 2; $return = "Array\n" . ToString::indent( "(\n", $indent ); $indent++; foreach ( $array as $key => $value ) { $result = ToString::_varToString( $value, $max_lines, $max_depth, $min_depth, $depth + 1, $lines, $lines_reserved + $count, $indent + 1 ); $count--; $lines += $result['lines']; $return .= ToString::indent( "[$key] => ". $result['text'] ."\n", $indent ); if ( ( ( $depth <= $min_depth && ( $lines + $depth ) > $max_lines ) || ( $depth > $min_depth && ( $lines + $lines_reserved ) >= $max_lines + $depth ) ) && ( $count != 1 ) ) { if ( $count > 0 ) { $return .= ToString::indent( "... ($count)\n", $indent ); $lines++; } break; } } $indent--; $return .= ToString::indent( ")", $indent ); } else { if ( ! empty( $array ) ) { $return = "Array($count)"; } else { $return = "Array()"; } } return array( 'text' => $return, 'lines' => ( $lines - $start_lines ) ); } /** * Indent current string * * @param string $string String to indent * @param int $indent Number of Indentations * @param string $tab What a tab should look like * @return string Indented String */ protected static function indent( $string, $indent, $tab = " " ) { $pre = ''; for ( $i = 0; $i < $indent; $i++ ) { $pre .= $tab; } return $pre . $string; } /** * Converts an Exception to string * * This try's to replicate PHP's __toString for exceptions, however, it doesn't cut off * long strings and also shows variables * @param Object $e Exception Object * @return string Exception as String */ public static function exception( $e ) { $class = get_class( $e ); $file = $e->getFile(); $lineNr = $e->getLine(); $message = $e->getMessage(); $code = ""; if ( $e->getCode() ) $code = ' and code \''. $e->getCode() .'\''; $exceptionAsString = "Exception '$class' with message '$message'$code in $file:$lineNr\nStack trace:\n\n"; $exceptionAsString .= ToString::trace( $e->getTrace() ); return $exceptionAsString; } /** * Convert exception trace array to string * * @param array/object $trace Exception or trace array to convert to string * @return string String of Exception trace */ public static function trace( $trace ) { if ( is_object( $trace ) ) { $trace = $trace->getTrace(); } if ( ! is_array( $trace ) ) { return false; } $traceAsString = ""; $count = 0; foreach ( $trace as $frame ) { $args = ""; $expanded_args = ""; if ( isset( $frame['args'] ) ) { $args = array(); foreach ( $frame['args'] as $arg ) { $args[] = ToString::variable( $arg, 1 ); foreach( preg_split( "/(\n)/", ToString::variable( $arg ) ) as $line ) $expanded_args .= "# $line\n"; } $args = join( ", ", $args ); } if ( isset( $frame['file'] ) ) $format = "#%s %s(%s): %s(%s)\n%s\n\n"; else $format = "#%s [internal function]: %4\$s(%5\$s)\n%6\$s\n\n"; $traceAsString .= sprintf( $format, $count, isset( $frame['file'] ) ? $frame['file'] : 'unknown file', isset( $frame['line'] ) ? $frame['line'] : 'unknown line', ( isset( $frame['class'] ) ) ? $frame['class'].$frame['type'].$frame['function'] : $frame['function'], $args ? " $args " : "", $expanded_args ); $count++; } return $traceAsString; } } echo ToString::exception( new Exception( 'test', 123 ) );

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.40.0040.01119.11
8.3.30.0110.00420.38
8.3.20.0080.00020.38
8.3.10.0050.00222.09
8.3.00.0040.00422.44
8.2.170.0000.01522.96
8.2.160.0100.00722.12
8.2.150.0000.00824.18
8.2.140.0080.00024.66
8.2.130.0030.01026.16
8.2.120.0050.00319.95
8.2.110.0050.00522.20
8.2.100.0080.00417.85
8.2.90.0040.00419.34
8.2.80.0000.00819.34
8.2.70.0060.00317.88
8.2.60.0040.00418.29
8.2.50.0060.00618.07
8.2.40.0050.00318.27
8.2.30.0040.00418.20
8.2.20.0080.00017.89
8.2.10.0030.00517.89
8.2.00.0060.00617.82
8.1.270.0080.00024.66
8.1.260.0000.01426.35
8.1.250.0050.00328.09
8.1.240.0090.00024.02
8.1.230.0000.01219.14
8.1.220.0000.00817.91
8.1.210.0040.00418.77
8.1.200.0070.00317.48
8.1.190.0040.00417.66
8.1.180.0050.00318.10
8.1.170.0040.00418.71
8.1.160.0040.00419.02
8.1.150.0040.00418.60
8.1.140.0040.00417.61
8.1.130.0000.00717.97
8.1.120.0040.00417.48
8.1.110.0000.00917.59
8.1.100.0040.00417.43
8.1.90.0040.00417.55
8.1.80.0000.00717.53
8.1.70.0040.00417.51
8.1.60.0060.00317.72
8.1.50.0030.00517.59
8.1.40.0040.00417.58
8.1.30.0040.00417.73
8.1.20.0060.00317.74
8.1.10.0040.00417.59
8.1.00.0040.00817.58
8.0.300.0000.00718.77
8.0.290.0070.00017.14
8.0.280.0070.00018.47
8.0.270.0080.00017.26
8.0.260.0070.00016.82
8.0.250.0070.00017.03
8.0.240.0060.00317.01
8.0.230.0030.00317.02
8.0.220.0030.00616.89
8.0.210.0000.00716.98
8.0.200.0000.00717.04
8.0.190.0000.00817.06
8.0.180.0000.00817.00
8.0.170.0060.00316.90
8.0.160.0000.00717.06
8.0.150.0070.00016.94
8.0.140.0080.00017.02
8.0.130.0030.00313.36
8.0.120.0030.00616.84
8.0.110.0040.00417.04
8.0.100.0040.00416.89
8.0.90.0000.00716.80
8.0.80.0090.01317.00
8.0.70.0000.00716.87
8.0.60.0000.00816.97
8.0.50.0000.00816.97
8.0.30.0080.01417.25
8.0.20.0120.00717.40
8.0.10.0030.00517.08
8.0.00.0080.01016.91
7.4.330.0060.00015.03
7.4.320.0060.00016.50
7.4.300.0000.00616.74
7.4.290.0000.00816.58
7.4.280.0030.00516.59
7.4.270.0020.00516.52
7.4.260.0000.00613.29
7.4.250.0000.00716.60
7.4.240.0050.00216.53
7.4.230.0000.00716.76
7.4.220.0090.00916.61
7.4.210.0050.01016.64
7.4.200.0000.00716.48
7.4.190.0000.00716.73
7.4.160.0170.00316.61
7.4.150.0060.01317.40
7.4.140.0070.01417.86
7.4.130.0080.01016.69
7.4.120.0090.00916.63
7.4.110.0070.01016.80
7.4.100.0190.00316.62
7.4.90.0070.01116.64
7.4.80.0140.01019.39
7.4.70.0080.01016.38
7.4.60.0090.01516.39
7.4.50.0030.00516.63
7.4.40.0120.00622.77
7.4.30.0100.01416.79
7.4.00.0110.00414.89
7.3.330.0030.00313.41
7.3.320.0030.00313.19
7.3.310.0050.00216.42
7.3.300.0050.00216.49
7.3.290.0030.01016.41
7.3.280.0070.01016.40
7.3.270.0070.01017.40
7.3.260.0070.01016.57
7.3.250.0100.00816.54
7.3.240.0050.01116.49
7.3.230.0120.00616.69
7.3.210.0100.00716.71
7.3.200.0170.00319.39
7.3.190.0060.01316.41
7.3.180.0070.01316.67
7.3.170.0070.01616.61
7.3.160.0030.01816.48
7.3.120.0060.01115.06
7.3.110.0060.01115.05
7.3.100.0000.01415.20
7.3.90.0030.00915.00
7.3.80.0000.01715.06
7.3.70.0060.00314.96
7.3.60.0000.01415.09
7.3.50.0090.00314.93
7.3.40.0080.00814.54
7.3.30.0120.00414.60
7.3.20.0100.00616.88
7.3.10.0030.01016.93
7.3.00.0060.00616.87
7.2.330.0160.00316.69
7.2.320.0090.00916.56
7.2.310.0060.01616.96
7.2.300.0100.01016.76
7.2.290.0130.01016.68
7.2.250.0070.01315.12
7.2.240.0090.00915.34
7.2.230.0070.01015.36
7.2.220.0080.00415.06
7.2.210.0060.00615.09
7.2.200.0040.00815.34
7.2.190.0070.00715.17
7.2.180.0000.01415.18
7.2.170.0060.00315.30
7.2.00.0100.00719.39
7.1.330.0060.00915.57
7.1.320.0060.00615.98
7.1.310.0050.00515.65
7.1.300.0070.01015.84
7.1.290.0090.00415.96
7.1.280.0000.01115.73
7.1.270.0060.00615.90
7.1.260.0140.00015.80
7.1.200.0040.01115.96
7.1.100.0060.00618.16
7.1.70.0100.00717.19
7.1.60.0130.01319.43
7.1.50.0030.01417.13
7.1.00.0030.03722.54
7.0.200.0040.00716.82
7.0.140.0030.06722.21
7.0.100.0030.08720.02
7.0.90.0030.09020.02
7.0.80.0130.07719.96
7.0.70.0070.08319.97
7.0.60.0070.08720.05
7.0.50.0070.04320.29
7.0.40.0000.05020.14
7.0.30.0100.03720.14
7.0.20.0070.04020.04
7.0.10.0030.04320.16
7.0.00.0030.04320.07
5.6.280.0030.07720.86
5.6.250.0100.08320.62
5.6.240.0100.08720.77
5.6.230.0130.07320.64
5.6.220.0170.06320.57
5.6.210.0070.08320.70
5.6.200.0100.03721.13
5.6.190.0200.04721.10
5.6.180.0070.04021.07
5.6.170.0100.03721.09
5.6.160.0130.03721.05
5.6.150.0100.03721.13
5.6.140.0130.03321.12
5.6.130.0170.04321.05
5.6.120.0070.04321.03
5.6.110.0070.09021.15
5.6.100.0030.08721.17
5.6.90.0170.03021.07
5.6.80.0000.04020.58
5.6.70.0070.04020.43
5.6.60.0100.03320.49
5.6.50.0100.04720.50
5.6.40.0100.03320.36
5.6.30.0000.04720.44
5.6.20.0070.03720.45
5.6.10.0100.03720.51
5.6.00.0170.02720.45
5.5.380.0130.08020.44
5.5.370.0070.07020.40
5.5.360.0100.08020.46
5.5.350.0100.08320.45
5.5.340.0030.04320.88
5.5.330.0100.03720.98
5.5.320.0030.04720.83
5.5.310.0030.04020.86
5.5.300.0000.05720.93
5.5.290.0030.04320.83
5.5.280.0030.07720.85
5.5.270.0000.06720.96
5.5.260.0100.03720.93
5.5.250.0030.06020.51
5.5.240.0100.08020.32
5.5.230.0030.04020.31
5.5.220.0070.03720.29
5.5.210.0000.04320.20
5.5.200.0100.03320.17
5.5.190.0000.04320.20
5.5.180.0130.03020.20
5.5.160.0070.03720.04
5.5.150.0030.03720.26
5.5.140.0200.02320.16
5.5.130.0030.04020.28
5.5.120.0070.04720.29
5.5.110.0100.04020.15
5.5.100.0070.03720.23
5.5.90.0000.04020.14
5.5.80.0030.04020.10
5.5.70.0170.02720.11
5.5.60.0000.04320.13
5.5.50.0100.04320.21
5.5.40.0200.06320.20
5.5.30.0100.07720.07
5.5.20.0130.07720.09
5.5.10.0100.07320.16
5.5.00.0130.07020.06
5.4.450.0000.04319.50
5.4.440.0070.04319.21
5.4.430.0000.06019.27
5.4.420.0030.08719.55
5.4.410.0130.04019.40
5.4.400.0100.03019.05
5.4.390.0070.03719.05
5.4.380.0030.03719.03
5.4.370.0030.03719.04
5.4.360.0000.04019.05
5.4.350.0100.03318.92
5.4.340.0030.04019.04
5.4.320.0130.03719.06
5.4.310.0030.03019.20
5.4.300.0100.03719.04
5.4.290.0070.03318.87
5.4.280.0030.04019.13
5.4.270.0000.04019.03
5.4.260.0070.03718.92
5.4.250.0130.02719.05
5.4.240.0000.03719.13
5.4.230.0000.04019.15
5.4.220.0030.03719.04
5.4.210.0070.05718.91
5.4.200.0070.07319.04
5.4.190.0030.07719.20
5.4.180.0030.03719.12
5.4.170.0100.06719.08
5.4.160.0000.07719.00
5.4.150.0070.04718.98
5.4.140.0130.07016.34
5.4.130.0030.04016.41
5.4.120.0100.05716.45
5.4.110.0030.05316.52
5.4.100.0100.06316.32
5.4.90.0070.07316.54
5.4.80.0070.04316.52
5.4.70.0030.07316.46
5.4.60.0070.07716.34
5.4.50.0130.06016.46
5.4.40.0100.04716.45
5.4.30.0070.04716.38
5.4.20.0100.07316.35
5.4.10.0130.06716.42
5.4.00.0070.04315.86
5.3.290.0030.03014.84
5.3.280.0000.04014.58
5.3.270.0100.07014.64
5.3.260.0030.08014.61
5.3.250.0000.07714.59
5.3.240.0100.07014.63
5.3.230.0100.07314.68
5.3.220.0070.07314.59
5.3.210.0100.04314.64
5.3.200.0030.08014.74
5.3.190.0100.04014.60
5.3.180.0000.04714.55
5.3.170.0130.03314.68
5.3.160.0030.07714.68
5.3.150.0130.06014.56
5.3.140.0000.08314.59
5.3.130.0070.04314.55
5.3.120.0030.08714.66
5.3.110.0130.07014.62
5.3.100.0030.08014.11
5.3.90.0030.06714.22
5.3.80.0200.06013.96
5.3.70.0100.07314.13
5.3.60.0130.07014.11
5.3.50.0070.07313.98
5.3.40.0070.06314.11
5.3.30.0100.06714.05
5.3.20.0070.07313.84
5.3.10.0030.07313.80
5.3.00.0070.04313.82

preferences:
65.69 ms | 400 KiB | 5 Q