I’m currently trying to improve my PHP skills and I have a sample below I am trying to work with. The first webpage (not shown here) is a form that contains one field, Name:, which then gets submitted to another page (shown below), where the form is processed.
For some reason the PHP code appears to be validated, but it is not outputting anything when I run a test. The input name for the field on the first page is “name”, then the information is sent to the PHP file shown below. Then the file is supposed to compare the time of day and then output a message based on the date information gathered. It’s not working though.
(Also, I know that the bgcolor attribute is deprecated, and I found the example online)
Any help would be appreciated. Thanks, Rob
<!DOCTYPE html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Databases and Webprogramming: Assignment 4.1</title>
</head>
<body bgcolor="#f9f9f9">
<?php
//import form information
$name = $_POST['name'];
$current = date('H');
if ($current < 12)
echo "Good morning, $name!<br />";
elseif ($current==12 && $current<=18)
echo "Good afternoon, $name!<br />";
elseif ($current >= "19" && $current <= "24")
echo "Good evening, $name!<br />";
?>
</body>
</html>
As you say you dont see any output the error is most likely located around
$current. Otherwise you should at least see the static part of the output strings. I only see one mistake in the second condition.$current==12should be$current>=12. Depending on your timezone this can fix the issue because the code as is cant output anything between 13 and 18 o’clock.If this does not fix it try using curved parantheses to avoid overseeing errors where you misplaced semicolons. Maybe you want to print the value of
$currentoutside of the if conditions and debug the code a bit.Edit: Oh and you should not use numbers as string (e.g.
"19"). If you want to be type safe you can write$current = (int) date('H');Edit #2: I just recognized those dozens of comments and found the link. When I type in
testand submit the form I getGood evening, test!. Note that the script is using the timezone of the server. I should actually see a friendly “good morning” as it is 2am in the morning over here.