Current Location: Home> Latest Articles> PHP Practical Tutorial: Easily Calculate Odd Numbers within 100

PHP Practical Tutorial: Easily Calculate Odd Numbers within 100

M66 2025-07-26

PHP Programming Practice: Calculating Odd Numbers Within 100

Mastering Basic Number Processing

Handling numeric data is a common task in PHP programming. This article will show you how to use PHP code to calculate and output all odd numbers within 100, along with a practical example to help you easily understand the logic.

Definition and Identification of Odd Numbers

Odd numbers are integers that cannot be evenly divided by 2. We can loop through numbers from 1 to 100 and check each one to see if it is odd, then output those that meet the condition.

PHP Code Example

<?php
// Loop through numbers from 1 to 100
for ($i = 1; $i <= 100; $i++) {
    // Check if the number is odd
    if ($i % 2 != 0) {
        echo $i . " ";
    }
}
?>

Code Explanation

This code uses a for loop to iterate from 1 to 100, checking each number whether it is divisible by 2. By using the modulo operator "%" to check if the remainder is not zero, it identifies odd numbers. If true, it outputs the number with a space separator.

Execution Result

Running this code will output: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99.

Summary

This example shows how PHP can use loops combined with conditionals to perform simple numeric filtering tasks. Mastering these basic programming techniques is helpful for deeper PHP learning and practical development.

Learning Advice

We hope this article provides useful reference for PHP beginners. Consistent practice and hands-on experience will help you quickly improve your coding skills and solve more real-world problems.