Learning php and I had to write this basic script :
<!DOCTYPE html>
<html>
<head>
<title>Whats My Age</title>
</head>
<body>
<form method="get">
Year of Birth: <input type="text" name="age" />
<input type="submit" name="submit" />
</form>
<?php
function isNegative()
{
if ($_GET['age'] < 0)
{
echo "Invalid Number";
}
}
isNegative();
function showMsg()
{
if (is_numeric($_GET['age']) && ($_GET['age'] >= 1) && isset($_GET['submit']))
{
$year = htmlentities($_GET['age']);
$asd1 = $year + 1900;
$today = date('Y');
$now = $today - $asd1;
echo "You are " . $now . " years old";
}
}
showMsg();
?>
</body>
</html>
Got two questions
How can the script run after the user has written something in the input, since the moment you open the file, it gives an error that
$_GET['age']
is undefined, which obviously is so or should I not worry about that as its only server side and the front end stuff will fix that ? For the same reasoning if I put the code inshowMsg()
outside of the function and put anelse {}
statement, the else runs as the page opens.I'm supposed to turn a short user input like - "58 " to the complete year such as 1958. I find a problem with that however as if the user puts in 01 for example, how would the script know if its 1901 or 2001, which is probably a problem in the condition of the task ?