Check Natural Number in PHP

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 2 Code Examples
Integers

What you’ll learn

  • A down-to-earth meaning of natural number (counting numbers) and the test n > 0 on integers.
  • A tiny helper isNatural with a clear PHP output message.
  • A second program that prints 1 through 10 in one line, and a live preview to try your own value.

Overview

If you can answer “how many?” with 1, 2, 3, ... you are using natural numbers in the everyday sense. The program only needs to know: is this integer strictly positive?

Yes / no check

One comparison: num > 0 for the sample definition.

Range listing

Print 1 2 ... 10 as a second pattern.

Live preview

Type an integer and see natural or not under our rule.

Prerequisites

if, for, echo, and integer types.

  • Basic PHP syntax and writing small reusable functions.
  • Comfort reading comparisons like > and <=.

The idea

Store the value as an int. If it is greater than zero, treat it as natural for this lesson. Zero and negatives fail the test; fractions are not integers, so they are out of scope here.

That single rule is enough for many homework-style “is it natural?” problems.

Live preview

Uses the same rule as the code: natural means integer > 0.

Try 42, 0, or -3. Non-integers show a short error message.

Live result
Press “Check”.

Algorithm

Goal: decide whether num is a natural number under the rule num > 0.

Read or fix num

Use a literal (example 1) or read CLI input with fgets(STDIN).

Test

If num > 0, answer yes; otherwise no.

Print

Print a sentence a human can read.

📜 Pseudocode

Pseudocode
function isNatural(num):   // integer
    if num > 0:
        return true
    return false
1

Yes / no for one integer

Same structure as the reference: helper check with sample value 42 and a clear yes/no output line.

php
<?php
function isNatural(int $num): bool
{
    return $num > 0;
}

$number = 42;

if (isNatural($number)) {
    echo $number . " is a natural number.\n";
} else {
    echo $number . " is not a natural number.\n";
}
?>

Explanation

Change number to 0 or a negative value to see the other branch. The ternary form is compact; an if (num > 0) return 1; return 0; style reads aloud the same way.

2

Print from 1 to 10 in order

Lists the counting numbers from 1 through 10 on one line - handy when you want to show a slice of the naturals without testing each value.

php
<?php
function printNaturalsInRange(int $start, int $end): void
{
    echo "Natural numbers in the range $start to $end:\n";
    for ($i = $start; $i <= $end; $i++) {
        if ($i > 0) {
            echo $i . " ";
        }
    }
    echo "\n";
}

printNaturalsInRange(1, 10);
?>

Notes

Range printing. Example 2 assumes start and end make sense for your task. If start could be below 1, either clamp it or filter so you only print true naturals under your definition.

User input. When you read with fgets(STDIN), validate numeric input before calling isNatural.

❓ FAQ

In this tutorial, it is a whole number you get when you count real things: 1, 2, 3, and so on. We treat "natural" as the same as "positive integer" (strictly greater than zero).
It depends on the book. Some definitions include 0; this program uses the test n > 0, so 0 is not natural here. If your course includes 0, change the test to n >= 0 and rename the message text to match your definition.
PHP supports booleans directly, so returning true for yes and false for no keeps the helper clear and readable.
The function takes an int. Numbers like 1.5 are not integers, so you would use a different type and different rules (this page stays with whole numbers).
They fail the n > 0 test, so the program says they are not natural numbers under our definition.
A single comparison is O(1) time. Printing a range from a to b costs O(b - a + 1) time for the loop body.

🔄 Input / output

Both programs use fixed values to keep output predictable. You can use trim(fgets(STDIN)) when you want user input.

Edge cases

Zero

num == 0

Not natural under n > 0; may be natural if your syllabus includes 0 - adjust the test and wording together.

Overflow

Very large int

Still an integer; the comparison works. If you need bigger values, confirm integer limits for your PHP runtime.

⏱️ Time and space complexity

OperationTimeExtra space
Single testO(1)O(1)
Print range start..endO(end - start + 1)O(1)

Summary

  • Definition used: integer n with n > 0.
  • Patterns: one-off check plus optional range listing.
  • Remember: align code with the definition your exam uses (especially for 0).
Did you know?

In many school-level texts, natural numbers start at 1 (1, 2, 3, ...). Some mathematicians include 0; programs must follow whichever definition your teacher or spec states - here we use n > 0.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful