<?php
function even($nr) {
if ($nr % 2) { // % = modulus - ex. 2 % 2 = 0 3 % 2 = 1
$result = 0;
} else {
$result = 1;
}
return $result;
}
// example of use
for ($x = -5 ; $x < 5 ; $x++) {
if (even($x)) {
printf("%s is an even number<br>\n",$x);
} else {
printf("%s is an odd number<br>\n",$x);
}
}
?>
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query Categories : PHP , PHP , HTML , PDF , Excel How to implement a session tracking system. Categories : PHP , Sessions , Variables Problem with Database Connectivity in PHP and MS Access? Don't
worry, The easy way is...... Categories : ADO , PHP , Databases STR - a Perl-like string manipulator class - The str class provides 4 perl-like methods for manipulating strings and
other scalar variables. Categories : PHP , PHP Classes , Perl , Strings Popup window creator for images on the Fly. Categories : PHP , GD image library , Java Script PHP based Contact email form with multiple recipients, text file based, supports departments. Categories : PHP , Email , Beginner Guides , Filesystem SubmitForce URL power submitter (searchengine submission class) Categories : PHP , Search Engines , URLs , PHP Classes usercounter class Categories : PHP , PHP Classes , Databases , MySQL , Environment Variables Functions to access a NNTP/NNRP newsserver. Complete set with examples is at ftp://ftp.nederland.net/pub/nnrplib Categories : Search , PHP , Complete Programs Script to check values being submitted by POST or GET method from a form. This script may help diagnose what variables are being supplied by a browser to other php scripts. Categories : HTML , Variables , Debugging , PHP , HTTP http://phpMySearch.web4.hm - The phpMySearch search engine system is a completeworld wide web indexing and searching system for a small domain or intranet. Categories : Search Engines , PHP , Databases , MySQL Create images on fly using GD-Image Lib Categories : PHP , Graphics bindtextdomain -- Sets the path for a domain Categories : PHP , PHP Functions , gettext Link Manager for Link Exchangers Categories : PHP , PHP Classes , Databases , MySQL , CURL Most of the browsers, especially Internet Explorer, behave in different ways. Hence it become necessary to use Browser detection to fix the non standard behavior of the browser. This is a browser sniffer class that can be used for the above purpose. Categories : PHP , PHP Classes , Browsers
Ojas Patel wrote : 179
While the Mr. Kamp`s example works fine, it is unnecessarily
complex; this works fine:
function even($nr) {
return ($nr % 2);
}
But, do we really need to write a function to do this?