3v4l.org

run code in 300+ PHP versions simultaneously
#!/usr/bin/env php <?php /** * Generate the page order for building a PDF of A4 pages printed as an A5 Booklet form on a MacBook that is * missing the proper A5 Booklet form. * * @return int[] */ function getPageList(int $pageCount): array { // Generate a list of page numbers, including additional blank pages required to get the total number of page // divisible by 4 to represent the list of A4 pages to be printed in as an A5 Booklet form. $pages = $pageCount % 4 !== 0 ? array_merge( range(1, $pageCount), array_fill( $pageCount + 1, 4 - ($pageCount % 4), 'blank' ) ) : range(1, $pageCount); $results = []; // Extract the pages to be printed in the relevant order for an A5 Booklet form. while (count($pages) !== 0) { // The page order is last page, first page, first + 1, last - 1. // As we remove these pages from $pages, we can repeat this extraction whilst there are no pages to left. $results[1 + count($results)] = [ 'Left front' => array_pop($pages), 'Right front' => array_shift($pages), 'Right back' => array_shift($pages), 'Left back' => array_pop($pages), ]; } return $results; } print_r(getPageList($argv[1]));
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Warning: Undefined array key 1 in /in/Ov06C on line 43 Fatal error: Uncaught TypeError: getPageList(): Argument #1 ($pageCount) must be of type int, null given, called in /in/Ov06C on line 43 and defined in /in/Ov06C:10 Stack trace: #0 /in/Ov06C(43): getPageList(NULL) #1 {main} thrown in /in/Ov06C on line 10
Process exited with code 255.

preferences:
87.63 ms | 402 KiB | 60 Q