Page 1 of 1

get age script error!

Posted: Tue Sep 23, 2014 8:49 pm
by cap2cap10
Hello again, members of the PHP Technorati. Again I come seeking your guidance on the subject of an age script that I wrote.
Two problems are occurring:

1- here is the error:

PHP Fatal error: Call to undefined function get_age() in C:\xampp\htdocs\test\test_age.php on line 187

2- if the current month is less than the birth month then it should be the tentative_age minus 1, but it is ignoring the arguement :banghead:

Here is the code:

Code: Select all

<html>
<head>
  <title>Get AGE!</title>
</head>

<body>
<div>
 <form method=POST action="<?php echo$_SERVER['PHP_SELF'];?>">

 <b>Enter your Birth date:</b> <br><br>
 MONTH:
  <select name= bmonth >
  <option></option>
<option value="1">JAN</option>
<option value="2">FEB</option>
 <option value="3">MAR</option>
 <option value="4">APR</option>
<option value="5">MAY</option>
 <option value="6" >JUN</option>
<option value="7">JUL</option>
<option value="8">AUG</option>
<option value="9">SEP</option>
<option value="10">OCT</option>
 <option value="11">NOV</option>
<option value="12" >DEC</option>
</select>
 <br>DAY:
 <select name= bday >
  <option></option>
<option value="1">1</option>
<option value="2">2</option>
 <option value="3">3</option>
 <option value="4">4</option>
<option value="5">5</option>
 <option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
 <option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
 <option value="15">15</option>
 <option value="16">16</option>
<option value="17">17</option>
 <option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
 <option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
 <option value="27">27</option>
 <option value="28">28</option>
<option value="29">29</option>
 <option value="30">30</option>
<option value="31">31</option>
</select>
 <br>YEAR:
 <select name= byear >
  <option></option>
  <option value="2020">2020</option>
  <option value="2019">2019</option>
  <option value="2018">2018</option>
  <option value="2017">2017</option>
  <option value="2016">2016</option>
  <option value="2015">2015</option>
  <option value="2014">2014</option>
  <option value="2013">2013</option>
  <option value="2012">2012</option>
  <option value="2010">2010</option>
  <option value="2009">2009</option>
  <option value="2008">2008</option>
  <option value="2007">2007</option>
  <option value="2006">2006</option>
  <option value="2005">2005</option>
  <option value="2004">2004</option>
  <option value="2003">2003</option>
  <option value="2002">2002</option>
  <option value="2001">2001</option>
  <option value="2000">2000</option>
  <option value="1999">1999</option>
  <option value="1998">1998</option>
  <option value="1997">1997</option>
  <option value="1996">1996</option>
  <option value="1995">1995</option>
  <option value="1994">1994</option>
  <option value="1993">1993</option>
  <option value="1992">1992</option>
  <option value="1991">1991</option>
  <option value="1990">1990</option>
  <option value="1989">1989</option>
  <option value="1988">1988</option>
  <option value="1987">1987</option>
  <option value="1986">1986</option>
  <option value="1985">1985</option>
  <option value="1984">1984</option>
  <option value="1983">1983</option>
  <option value="1982">1982</option>
  <option value="1981">1981</option>
  <option value="1980">1980</option>
  <option value="1979">1979</option>
  <option value="1978">1978</option>
  <option value="1977">1977</option>
  <option value="1976">1976</option>
  <option value="1975">1975</option>
  <option value="1974">1974</option>
  <option value="1973">1973</option>
  <option value="1972">1972</option>
  <option value="1971">1971</option>
  <option value="1970">1970</option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>
  <option value=""></option>




</select>
<input type=hidden name=calc_age value=true />
	<input type=submit name=submit value=Submit />

</form></div><br><br><br>

<div>
<textarea cols="30" rows="1" wrap="OFF" maxlength="30" readonly="readonly" name="bday"/>
<?php
if (isset($_POST['calc_age'])){
$bmonth = $_POST['bmonth'];
$bday = $_POST['bday'];
$byear = $_POST['byear'];

function get_age($byear, $bmonth, $bday) {
   $cur_year =  date('Y');
   $cur_day = date('d');
   $cur_month = date('m');
   $tentative_age = $cur_year - $byear ;
   if ($cur_month < $bmonth ){
   $age = $tentative_age - 1;

   }elseif ($cur_month == $bmonth && $cur_day < $bday){
   $age = $tentative_age - 1;

   }
   else{
   $age = $tentative_age ;
   }
   return "You current age is:  <b>".$age."</b> ";
}
 }else{}
echo get_age($byear, $bmonth, $bday);

 ?>
</textarea>
</div>

</body>

</html>
Please advise on the error of my logic.
Thanks in advance. :drunk:

Sincerely,

Batoe

Re: get age script error!

Posted: Tue Sep 23, 2014 9:21 pm
by requinix
Don't define functions inside anything conditional, particularly if blocks (like you're doing) or other functions. Put it at the very beginning of the script so it's easy to find, or better yet put it in a different file entirely, all by itself, and then require() that file.

Re: get age script error!

Posted: Tue Sep 23, 2014 9:40 pm
by cap2cap10
Ok, but I get the same error: (I moved the definitions outside of the function)

PHP Fatal error: Call to undefined function getAge() in C:\xampp\htdocs\test\get_age.php on line 25

when I place it in a separate file. It did work on the first try, but it ignores the first argument. If the month is less than the birth month, $age = $tentative age - 1.

batoe

Re: get age script error!

Posted: Tue Sep 23, 2014 11:35 pm
by requinix
Did you rename the function? That's not what it's called in the code you posted.

So I'm not sure if this is the cause of the problem you're describing, because I don't actually know what you're describing, but the couple less-than comparisons you're doing won't always work. It's happening because you're always comparing strings, not numbers, and as strings you get issues with how "10" < "9". If you're wondering why you have strings, date() always returns a string and values in $_POST are always strings.
Do some typecasting to fix that. Example:

Code: Select all

$bmonth = (int)$_POST['bmonth'];

Code: Select all

$cur_year = (int)date('Y');

Re: get age script error!

Posted: Wed Sep 24, 2014 12:57 am
by cap2cap10
Ok, Forget about the argument issue; it actually works is was me not understanding what I was posting. Fatigue lol.
But the I cannot seem to get rid of that error. The function is being called correctly so Why the Fatal error? Is it my editor?
The logic seems sound to me. Granted I could always hide the error on that page or script by error_reporting (0). But I would still like to know why it is behaving this way. And yes I thought I screwed up the naming convention for functions so I renamed it to no avail.

Thanks for your patience,

Batoe

Re: get age script error!

Posted: Wed Sep 24, 2014 1:42 am
by requinix
cap2cap10 wrote:But the I cannot seem to get rid of that error. The function is being called correctly so Why the Fatal error? Is it my editor?
Did you read the first line in my post?
cap2cap10 wrote:The logic seems sound to me. Granted I could always hide the error on that page or script by error_reporting (0). But I would still like to know why it is behaving this way. And yes I thought I screwed up the naming convention for functions so I renamed it to no avail.
Did you read everything after the first line in my post?

Re: get age script error!

Posted: Wed Sep 24, 2014 4:08 am
by cap2cap10
I'm so sorry, mental fatigue is setting in. :oops: lol Ok Do not define functions in any conditional statement. I totally read pass that.
Problem vanished, hence solved. :D

Thanks again for your patience.

Batoe