Asked  7 Months ago    Answers:  5   Viewed   41 times

What is the simplest most basic way to find out if a number/variable is odd or even in PHP? Is it something to do with mod?

I've tried a few scripts but.. google isn't delivering at the moment.



You were right in thinking mod was a good place to start. Here is an expression which will return true if $number is even, false if odd:

$number % 2 == 0

Works for every integerPHP value, see as well Arithmetic OperatorsPHP.


$number = 20;
if ($number % 2 == 0) {
  print "It's even";


It's even

Wednesday, March 31, 2021
answered 7 Months ago

You can basically do that, but you have to put the parens on the outside.

$var1 = 'parent';
$var2 = 'available_from';
$keyValuePairs[$key] = $item->{$var1}()->{$var2};
// or $keyValuePairs[$key] = $item->$var1()->{$var2};

And there basically is no way of getting around that without using eval:

// escape the first $
$keyValuePairs[$key] = eval( "$item->$var1->$var2" );

But, there is really no reason to use eval if you have access to the potential set of variables first.

You can do something like this to get around it:

function call_or_return( $obj, $prop )
    // test to see if it is a method (you'll need to remove the parens first)
    $arr = array( $obj, $prop );
    // if so call it.
    if( is_callable( $arr ) ) return call_user_func( $arr );
    // otherwise return it as a property
    return $obj->$prop;

call_or_return( $item, $var1 )->{$var2};
Saturday, May 29, 2021
answered 5 Months ago

Use modulus:

function isEven(n) {
   return n % 2 == 0;

function isOdd(n) {
   return Math.abs(n % 2) == 1;

You can check that any value in Javascript can be coerced to a number with:


This check should preferably be done outside the isEven and isOdd functions, so you don't have to duplicate error handling in both functions.

Monday, June 7, 2021
answered 5 Months ago

I found this code here:

Optimized and prettified function:

 * @param int $number
 * @return string
function numberToRomanRepresentation($number) {
    $map = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400, 'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40, 'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
    $returnValue = '';
    while ($number > 0) {
        foreach ($map as $roman => $int) {
            if($number >= $int) {
                $number -= $int;
                $returnValue .= $roman;
    return $returnValue;
Sunday, August 8, 2021
answered 3 Months ago
set /a num=%random% %%100 +1
SET /a nummod2=num %% 2
IF %nummod2% == 0 (ECHO %num% is even) ELSE (ECHO %num% is odd)


Conventional IF syntax is if [not] operand1==operand2 somethingtodo where operand1 and operand2 are both strings. If the strings contain separators like Space,Tab or other characters that have a special meaning to batch then the string must be "enclosed in quotes".

Fundamentally, if compares strings. The operator must be one of a fixed set of operators [== equ neq gtr geq lss leq] hence cmd was objecting to num where it expected an operator.

A calculation cannot be performed within a if statement's parameters.

%% is required to perform the mod operation, since % is a special character that itself needs to be escaped by a %.

Note that { and } are ordinary characters with no special meaning to batch and remarks must follow

rem remark string

There is a commonly-used exploit which is


actually, a broken label, which can have unforeseen side-effects (like ending a code-block)

Wednesday, September 1, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :