Jump to content

[SOLVED] Simple math script not executing fully; not giving off error.


Alecdude

Recommended Posts

Here's the script:

 

<?php
$age1 = "".$_POST['Year']." = ".$_POST['Year']." - 1996";
$age2 = "".$_POST['Year']." = 1996 - ".$_POST['Year']."";

if ($_POST['Submit']) {
if ($_POST['Year'] == 1996) {
echo "You are the same age as me!";
} else {
	if ($_POST['Year'] < 1996) {
		echo "You are ".$age1." year(s) younger than me";
	} else {
		echo "You are ".$age2." year(s) older than me";
	}
}
} ?>

 

I'm probably making a simple mistake. If anything else is needed, I will provide it. Please help.

Your making that a bit too complicated.

<?php
$age = abs($_POST['Year'] - 1996);

if ($_POST['Submit']) {
   if ($_POST['Year'] == 1996) {
   echo "You are the same age as me!";
   } else {
      if ($_POST['Year'] > 1996) {
         echo "You are ".$age." year(s) younger than me";
      } else {
         echo "You are ".$age." year(s) older than me";
      }
   }
} ?>

 

Sorry had to edit, didn't fully read what you were doing, plus your logic was wrong with the less than symbol.

Try this:

test.php

<?php
$age = abs($_POST['Year'] - 1996);

if ($_POST['Year'] == 1996) {
   echo "You are the same age as me!";
   } else {
      if ($_POST['Year'] > 1996) {
         echo "You are ".$age." year(s) younger than me";
      } else {
         echo "You are ".$age." year(s) older than me";
      }
   }
?>

 

test.html

<form action="test.php" method='POST'>
   Enter the Year in which you were born<input type='text' name='Year'><br/>
   <input type='Submit' value='Go'><br/>
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.