PHP – Check if a number is Even, Odd, or multiple of another
While coding PHP applications, there are times when we need to check if a number is even, odd, or multiple of another.
Here are a few methods to do the same.
Method 1:
$number = 3;
if($number & 1){
echo 'Odd number!';
}else{
echo 'Even number!';
}
Method 2:
$number = 3;
if($number % 2){
echo 'Odd number!';
}else{
echo 'Even number!';
}
Method 3:
$number = 3;
echo ($number % 2 ? 'Even' : 'Odd');
Method 4:
Check if the number is multiple of another. This can be used to float DIV elements and remove margin from the last ones.
for ($i=1; $i <= 10; $i++) {
if($i % 2){
echo $i.' is not multiple of 2';
}else{
echo $i.' is multiple of 2';
}
echo '<br />'; // Adds a line break.
}
If you know of another method, do share these in the comments.

Winery Website Development Services
Jewellery Website Development Services
Automotive Website Design Services
Drupal Website Development
Joomla Website Development
Webflow Development Services
Framer Website Development Services
WIX Development Services
Squarespace Development Services
AngularJS Website Development
ReactJS Development Services
IOS Apps Developmnet
Cross Platform App Development
Flutter App Development
Xamarin Apps Developmnet
Progressive Apps Developmnet
Prestashop Development Service
Neto (Maropost) Commerce Development Service
Shopify Plus Agency
Shopify Cro Services
Search Engine Optimization
Social Media Marketing
Paid Media Advertising
Google Ads Services
SEO Services India
Content Marketing Services
Email Marketing Services
SEO Pricing
Website Design Services India
Website Design Services USA Canada
Website Design Services British Columbia
Graphics Design Services
Logo Design
Figma To HTML5
Hire Remote Developers
Wearable App Development
Leave a Reply