PHP implementation: Get odd numbers within 100

王林
Release: 2024-03-10 13:46:01
Original
945 people have browsed it

PHP implementation: Get odd numbers within 100

PHP is a programming language that is widely used in Web development and has powerful functions and flexible syntax structures. In PHP, obtaining odd numbers within 100 can be achieved using loop structures and conditional judgments. The following is a simple PHP code example that demonstrates how to obtain all odd numbers within 100:

<?php
// 定义一个空数组,用于存储奇数
$oddNumbers = array();

// 循环遍历1到100的所有数字
for ($i = 1; $i <= 100; $i++) {
    // 判断当前数字是否为奇数
    if ($i % 2 !== 0) {
        // 如果是奇数,则将其添加到数组中
        $oddNumbers[] = $i;
    }
}

// 打印输出所有奇数
echo "100以内的奇数为:";
foreach ($oddNumbers as $number) {
    echo $number . " ";
}
?>
Copy after login

In this code, an empty array $oddNumbers is first defined to store all odd numbers within 100. Then iterate through all the numbers from 1 to 100 through a for loop, and judge whether each number is an odd number. If it is an odd number, add it to the array $oddNumbers. Finally, print out all odd numbers through a foreach loop.

The above is an example of using PHP code to obtain odd numbers within 100. This code can clearly demonstrate the basic syntax and array operations of PHP.

The above is the detailed content of PHP implementation: Get odd numbers within 100. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!