3v4l.org

run code in 500+ PHP versions simultaneously
<?php class NoConsumedFilter extends php_user_filter { function filter($in, $out, &$consumed, $closing) { while ($bucket = stream_bucket_make_writeable($in)) { // Do NOT update $consumed $bucket->data = strtoupper($bucket->data); stream_bucket_append($out, $bucket); } return PSFS_PASS_ON; } } class YesConsumedFilter extends php_user_filter { function filter($in, $out, &$consumed, $closing) { while ($bucket = stream_bucket_make_writeable($in)) { $consumed += $bucket->datalen; $bucket->data = strtoupper($bucket->data); stream_bucket_append($out, $bucket); } return PSFS_PASS_ON; } } stream_filter_register('no_consumed', 'NoConsumedFilter'); stream_filter_register('yes_consumed', 'YesConsumedFilter'); $filters = array('no_consumed', 'yes_consumed'); foreach ($filters as $filter) { $fp = tmpfile(); fwrite($fp, 'hello world'); rewind($fp); stream_filter_append($fp, $filter, STREAM_FILTER_READ); $data = fread($fp, 5); $pos = ftell($fp); $more = fread($fp, 100); $eof = feof($fp); echo $filter . ": first='" . $data . "' ftell=" . $pos . " rest='" . $more . "' eof=" . ($eof ? 'Y' : 'N') . "\n"; fclose($fp); }
Output for 8.1.0 - 8.1.34, 8.2.0 - 8.2.33, 8.3.0 - 8.3.33, 8.4.1 - 8.4.24, 8.5.0 - 8.5.9
Deprecated: Return type of NoConsumedFilter::filter($in, $out, &$consumed, $closing) should either be compatible with php_user_filter::filter($in, $out, &$consumed, bool $closing): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/p9d8S on line 5 Deprecated: Return type of YesConsumedFilter::filter($in, $out, &$consumed, $closing) should either be compatible with php_user_filter::filter($in, $out, &$consumed, bool $closing): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/p9d8S on line 18 no_consumed: first='HELLO' ftell=5 rest=' WORLD' eof=Y yes_consumed: first='HELLO' ftell=5 rest=' WORLD' eof=Y
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 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.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
no_consumed: first='HELLO' ftell=5 rest=' WORLD' eof=Y yes_consumed: first='HELLO' ftell=5 rest=' WORLD' eof=Y
Output for 4.3.2 - 4.3.11, 4.4.0 - 4.4.9
Fatal error: Class noconsumedfilter: Cannot inherit from undefined class php_user_filter in /in/p9d8S on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Fatal error: Class noconsumedfilter: Cannot inherit from undefined class php_user_filter in /in/p9d8S on line 3

preferences:
54.81 ms | 2872 KiB | 4 Q