PHP Script to Check Number is Even or Odd using If-else | Ternary Operator
PHP program to check whether a number is even or odd using an if-else statement.
In this article, you will learn how to make a PHP program to check whether a number is even or odd using an if-else statement.
Example
Input Number: 26
An input number is an even number.
You should have the knowledge of the following topics in PHP programming to understand this program:
- PHP
If-else
statement - PHP Ternary Operator
What are even odd numbers?
There is if any integer number divided by 2 and its reminder equals zero then it is an even number and if the remainder is greater than zero then it is an odd number.
Samples of even-odd Numbers
Source Code
<?php
// PHP Program to Check Whether a Number is Even or Odd using If-else Statement
$x = 26;
echo "Input Number: ", $x, "\n\n";
if ($x % 2 == 0) {
echo "The input number is even number.\n";
} else {
echo "The input number is odd number.\n";
}
?>
Output
Input Number: 26
The input number is even number.
Explanation
In this given program, we have taken input 26
from the user via the system console, Then we divided this input by 2
and checked that it's a reminder.
If its remainder is equal to zero then it's an even number otherwise odd number.
PHP Program to Check Whether a Number is Even or Odd using Conditional Operator
<?php
// PHP Program to Check Whether a Number is Even or Odd using Conditional Operator
$x = 27;
echo "Input Number: ", $x, "\n\n";
$x = $x % 2 == 0 ? 1 : 0;
if ($x) {
echo "The input number is even number.\n";
} else {
echo "The input number is odd number.\n";
}
?>
Output
Input Number: 27
The input number is odd number.