3v4l.org

run code in 300+ PHP versions simultaneously
<?php function test_position($input) { $_POST['position'] = $input; // Original code $original = ( isset( $_POST['position'] ) && (int) $_POST['position'] ) ? (int) $_POST['position'] : '-1'; // Alternative 1: !empty() $empty_check = ! empty( $_POST['position'] ) ? (int) $_POST['position'] : -1; // Alternative 3: null coalescing operator $null_coalesce = (int) ($_POST['position'] ?? '-1'); echo "Input: $input\n"; echo "Original: $original\n"; echo "!empty() check: $empty_check\n"; echo "Null coalescing: $null_coalesce\n"; echo "-------------------------\n"; } // Test cases $test_inputs = ['5', '0', 'xyz', '', null]; foreach ($test_inputs as $input) { test_position($input); } ?>
Output for 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
Input: 5 Original: 5 !empty() check: 5 Null coalescing: 5 ------------------------- Input: 0 Original: -1 !empty() check: -1 Null coalescing: 0 ------------------------- Input: xyz Original: -1 !empty() check: 0 Null coalescing: 0 ------------------------- Input: Original: -1 !empty() check: -1 Null coalescing: 0 ------------------------- Input: Original: -1 !empty() check: -1 Null coalescing: -1 -------------------------

preferences:
52.75 ms | 407 KiB | 5 Q