George Lamb
George Lamb

Reputation: 51

How to indicate if a number is odd or even in PHP in an HTML file

Im new to PHP and mysql, we have been set exercises in different rectangles in an HTML file. 2 rectangles in this exercise are

Rectangle 5: An "input field" that allows you to input a four digit integer number or a four letter string and store it in a variable "$x". A user will put the numbers/letters in a box and click on a button labelled "submit" in order to enter the number/letter into the variable.

Rectangle 6: An output that indicates if a number "$x" input by the user is even or odd.

I have got rectangle 5 to work and display variable $x a the top of the screen, however I cant seem to get rectangle 6 to work.

Here is my code so far:

function rect5if() { //rect5 if statement
    if( $_POST['name'] && $_POST['name'] != ""){
        $x = urldecode( $_POST['name'] );                
    } else {
        $x = "not set";
    }
    echo $x;
}//end of rect5

function rect6oddeven() {
    if(is_int($x/2)) {
        echo("Even");
    } else {
        echo("Odd");
    }
}

echo " <table border='1'>
<td>Rectangle 5 ".rect5if()."
<form method=\"post\" >
Enter four digit number/letter string
<input type=\"text\" name=\"name\" maxlength=\"4\">
<input type=\"submit\" />
</form></td> 
<td>Rectangle 6 ".rect6oddeven()."</td>
</tr>
</table> ";

Any suggestions? Thanks GL

Upvotes: 2

Views: 11329

Answers (4)

Przemek
Przemek

Reputation: 3985

You need to check what remainder of division by 2 will give you. If it will be 0, number is even, otherwise it's odd. Operator that does that is named modulus. I suggest you googling it to learn it.

Working code, assuming $x is is given value earlier in the code:

function rect6oddeven() {
  if ($x % 2 === 0) {
    echo("Even");
  }
  else {
    echo("Odd");
  }
}

Upvotes: 2

ScottSmudger
ScottSmudger

Reputation: 371

I don't really understand why it needs to be text. It does work either way. But you should use the html number attribute:

<input type=\"number\" name=\"name\" maxlength=\"4\" >

you could also use "min" and set that to 4. So the user would have to enter a 4 digit value.

Here is the function:

function rect6oddeven(){
    $x = urldecode( $_POST['name'] );
    if(is_numeric($x)){
        if($x % 2 == 0) {
            return "It's even";
        }else{
            return "It's odd";
        }
    }
}

Or even better, declare $x before you initiate any functions. Then you're doing both exercises in one.

<?
echo " <table border='1'>

<td>Rectangle 5 
<form method=\"post\" >
Enter four digit number/letter string
<input type=\"text\" name=\"name\" maxlength=\"4\">
<input type=\"submit\" />
</form></td> 


<td>Rectangle 6 ".rect6oddeven()."</td>
</tr>
</table> ";

$x = urldecode( $_POST['name'] );
function rect6oddeven(){
    if(is_numeric($x)){
        if($x % 2 == 0) {
            return "It's even";
        }else{
            return "It's odd";
        }
    }
}
?>

You should really work this out yourself, asking other people to work it out for you simply removes the joy of programming.

Upvotes: 0

Adam
Adam

Reputation: 6783

@frz3993 had it right. Your problem is scope. (Yes, using the % operator is better here, but it's not the main problem).

You define $x in one function. That variable is local to that function. In another function you can have $x but it's not the same $x. You could declare it global, but better to use a class since global variables are to be avoided whenever possible. For example:

<?php

class Page{
    private $x;

    function __construct(){
        if( $_POST['name'] && $_POST['name'] != ""){
          $this->x = urldecode( $_POST['name'] );                
        }else{
          $this->x = false; 
        }
    }

    function rect5if(){
        if( false === $this->x ) {
            return "not set";
        } else {
            return $this->x;
        }
    }

    function rect6oddeven(){
        if( false === $this->x ) {
            return '';
        } else {
            if( $this->x % 2 === 0 ) {
                return "even";
            } else {
                return "odd";
            }
        }
    }
}

$page = new Page();

?>
<table border='1'>
    <tr>
        <td>Rectangle 5 <?= $page->rect5if(); ?>
            <form method="post" >
                Enter four digit number/letter string
                <input type="text" name="name" maxlength="4">
                <button type="submit">Submit</button>
            </form>
        </td> 
        <td>Rectangle 6 <?= $page->rect6oddeven(); ?></td>
    </tr>
</table> 

The constructor reads in the data and stores it in the class variable x. Each of the functions can then access the same data since they are part of the same class.

Upvotes: 0

vinay kumar
vinay kumar

Reputation: 111

http://php.net/manual/en/language.operators.arithmetic.php

maybe not obvious use of the modulus (%) operator is to check if an integer is odd or even.

<?php
if (($a % 2) == 1)
{ echo "$a is odd." ;}
if (($a % 2) == 0)
{ echo "$a is even." ;}
?>

Upvotes: 2

Related Questions