note 59691 deleted from function.is-numeric by crell
| From: | crell@php.net | Date: | Mon, 18 Apr 2022 14:57:37 +0000 |
| Subject: | note 59691 deleted from function.is-numeric by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218280@lists.php.net to get a copy of this message | ||
Note Submitter: andrea dot vacondio at gmail dot com
----
Two simple functions using is_numeric:
<?php
function is_odd($num){
return (is_numeric($num)&($num&1));
}
function is_even($num){
return (is_numeric($num)&(!($num&1)));
}
//examples
echo "1: odd? ".(is_odd(1)? "TRUE": "FALSE")."<br />";
//is_numeric(0) returns true
echo "0: odd? ".(is_odd(0)? "TRUE": "FALSE")."<br />";
echo "6: odd? ".(is_odd(6)? "TRUE": "FALSE")."<br />";
echo "\"italy\": odd? ".(is_odd("italy")? "TRUE":
"FALSE")."<br />";
echo "null: odd? ".(is_odd(null)? "TRUE": "FALSE")."<br
/><br />";
echo "1: even? ".(is_even(1)? "TRUE": "FALSE")."<br
/>";
echo "0: even? ".(is_even(0)? "TRUE": "FALSE")."<br
/>";
echo "6: even? ".(is_even(6)? "TRUE": "FALSE")."<br
/>";
echo "\"italy\": even? ".(is_even("italy")? "TRUE":
"FALSE")."<br />";
echo "null: even? ".(is_even(null)? "TRUE": "FALSE")."<br
/>";
?>
And here is the result:
1: odd? TRUE
0: odd? FALSE
6: odd? FALSE
"italy": odd? FALSE
null: odd? FALSE
1: even? FALSE
0: even? TRUE
6: even? TRUE
"italy": even? FALSE
null: even? FALSE