Jump to content

Simple number check problem


apotd

Recommended Posts

Hi,

 

I'm totally new to PHP and I'm afraid I don't understand a lot yet. However, I need to make this: when you fill in a number in a form, it should be displayed whether the number is odd or even. I tried the following code, which is obviously not working and I have no idea on how to fix this.

 

<?php 
$var = $_POST['var'];

for ($i = 0; $i < $var; $i++) {
  if ($i % 2 == 0) {
    print("even");
  } else {
   print("odd");
  }
}
?>

Link to comment
Share on other sites

My bad, of course. I used the following code instead, which works:

 

<?php 
$number = $_POST['number'];

if($number % 2) {
print("odd");
} else {
    print("even");
}

?>

 

The thing is, I need to perform a for loop on the result (I won't be actually printing it in the end), so I was sort of confused by that. One more thing, is it possible just to put the for loop at the place where the "print" is now situated, or am I thinking totally wrong here?

Link to comment
Share on other sites

I'm sorry. I know I sound confusing. Fact is I am confused. But, I found out I had to use a while loop. Tried completing my code but I'm already having problems on the first part. I just want to do a calculation on the number if it is odd, and then make it print the outcome (end1) as long as the number is bigger than one. However, I get an error on the "$number / 2 = $end1;" line.

 

<?php 
$number = $_POST['number'];

if($number % 2) {
while ($number > 1){	
	$number / 2 = $end1;
	print($end1);
}
} else {
...
}

?>

Link to comment
Share on other sites

When I try that (your second post), it just divides the number in two. I need the script to keep dividing it in two till the number equals one. That's why I thought I should use a while loop to keep it going till that point. I also want it to print every step. But I don't see how I can do this without a loop. Is it wrong to use "$end1 = $number / 2; " as you first said? Because this actually removed the error, but it just prints an awful lot of 0's onto my page.

Link to comment
Share on other sites

Works like a charm, thank you!

 

One more thing. I cannot find any serious information on an else statement with a while loop, so I guess this is not possible. However, for an iteration of the code I wanted to create something like this:

 

while ($number % 2){	
   $number / 2;
- i know this is wrong but - } else {
   $number / 4;
    }

 

So that, as long as the number is even, it will divide itself by 2, otherwise by 4, and keeps doing this (so I was again thinking of the while loop), but I have no clue on how to do this to be honest.

Link to comment
Share on other sites

Would there be some workaround solution for this? For example another if statement within the while loop? Previously you said I should use this

while ($number > 1){	
echo $number . "<br />";
$number = $number / 2;
} 

for the particular code part. But I don't really see what made the loop end, since all that was added was the . "<br />, if I am not mistaken.

Link to comment
Share on other sites

<?php
if(is_numeric($_POST['var'])){
if($_POST['var']%2==0){
	echo 'even';
}else{
	echo 'odd';
}
}
?>

 

There are probably far more effective ways, but I'm not exactly sure how % does it, like looking at the number in binary numbers and see if it ends with 0 or 1, perhaps using one of the bitwise operators.

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

You could also compare the number divided by 2 and the number divided by 2 and rounded to nearest whole number.

Link to comment
Share on other sites

But I don't really see what made the loop end, since all that was added was the . "<br />, if I am not mistaken.

while ($number > 1){	
echo $number . "<br />";
$number = $number / 2;
}

 

the while loop says "Do this as long as $number is greater than 1".

inside the while loop, we say "replace the value of $number with whatever the value of $number/2 is".

ie.

pretend $number has an initial value of 30.

first time thru the loop $number is 30 - we echo it - then we replace 30 with 15

second time thru we echo 15 - then replace 15 with 7.5

third time - we echo 7.5 - then replace 7.5 with 3.75

fourth time - echo 3.75 - then replace 3.75 with 1.875

fifth time - echo 1.875 - then replace 1.875 with .9375

Since $number is now .9375, it is less then 1 (the condition we set in our WHILE) so the while loop ends

 

Clear?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.