<?php
function findMissingNumber($arr) {
// Calculate the expected sum of consecutive integers
$n = count($arr) + 1; // Including the missing number
$expectedSum = ($n * ($n + 1)) / 2;
// Calculate the actual sum of the given array
$actualSum = array_sum($arr);
// The missing number is the difference between expected sum and actual sum
$missingNumber = $expectedSum - $actualSum;
return $missingNumber;
}
// Example usage
$arr = [1, 2, 3, 5, 6, 7, 8];
echo "The missing number is: " . findMissingNumber($arr); // Output: The missing number is: 4
?>
Here’s a step-by-step guide on how to run the PHP script provided:
- Save it with a .php extension. For example, you can name it find_missing_number.php.
- Open the PHP File in a Text Editor: Use a text editor like Notepad, Sublime Text, or Visual Studio Code to open the find_missing_number.php file.
- Write or Copy and Paste the PHP Code: Copy the complete PHP code provided earlier in this conversation and paste it into the find_missing_number.php file.
- Save the PHP File: After pasting the code, save the find_missing_number.php file.
- Access the PHP Script: Open your web browser and type the following URL:
http://localhost/your_script_name.php
Replace your_script_name.php with the name of your PHP script file.

Algorithm to find missing number in PHP arrayConsecutive integers missing number PHPDetecting missing integer in PHP arrayPHP array missing number detectionPHP find missing number in arithmetic progressionPHP find missing number in arrayPHP function to find missing integer in sequence PHP code to detect missing number in seriesPHP program to find missing element in arrayPHP script to find the missing element in series