cSheffield90 wrote:Im trying to learn php so I picked up the book "Beginning php 5.3"
By Wrox? Keep in mind that book is more than 7 years old. Like, that XHTML Strict they're using fell out of fashion a few years ago.
You really should find something about 5.5 or 5.6 - not 6, and I wouldn't look at a 7 book quite yet.
Anyway, I know what the problem is but we need to teach you how to discover it yourself. Well, in principle, as this is a little different.
First of all, test using this code. I'll explain why later.
Code: Select all
<?php
$hour = date( "G" );
if ( $hour => 0 && $hour < 12 )
{
echo "<h1>It is before noon.</h1>";
}
else
{
echo "<h1>It is after noon.</h1>";
}
If you're seeing a 500 error then that means you are not seeing the error message(s) that PHP is creating. No error messages makes it much harder to figure out why stuff isn't working. In the real world you would be logging these errors to a file, but for now just showing them on the page is fine.
Find your php.ini on the Raspberry and look at the various settings inside. Somewhere in there are two of them, which you need to set to the below values:
Code: Select all
error_reporting = -1
display_errors = on
(Make sure you're editing the correct lines: ones starting with a semicolon are just comments and the real values are somewhere else in the file.)
Restart Apache and try running the code above. You should see an error message along the lines of
Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in (file) on line (line)
which refers to
You may or may not recognize the error immediately: using => when it should be >=. If you fix that and try again the script will (should) run properly.
If you don't see the error message then make sure you are not using IE/Edge. If you must use IE then
disable its "friendly error pages" setting. If you must use Edge then... well, it doesn't have any way to disable those pages so you really have to stop using Edge.
Anyway. Now that you have errors showing on the page, back to your original code. Try executing it now and you'll see an error about an unexpected <. It might also show the wrong line number. The code looks perfectly fine, right? Actually what you can
see looks right - the problem is what you can't see: non-breaking spaces. Typically made by pressing shift+space. There's one of them before the < and that causes problems for PHP, and there are more of them besides that. And those smart quotes you see with the strings and in some of the HTML? They will also cause problems.
Are you really typing the code in Notepad++? You're not typing it somewhere else, like Word? Make sure that is not using smart quotes and not allowing for non-breaking spaces. I don't know how to help you with that part since I don't use Notepad++. Try Google?
Here's your code with all the smart quotes and non-breaking spaces replaced with normal straight quotes and plain spaces, so you can test it out:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Greetings</title>
<link rel="stylesheet" type="text/css" href="common.css" />
</head>
<body>
<?php
$hour = date( "G" );
$year = date( "Y" );
if ( $hour >= 5 && $hour < 12 )
{
echo "<h1>Good morning!</h1>";
}
elseif ( $hour >= 12 && $hour < 18 )
{
echo "<h1>Good afternoon!</h1>";
}
elseif ( $hour >= 18 && $hour < 22 )
{
echo "<h1>Good evening!</h1>";
}
else
{
echo "<h1>Good night!</h1>";
}
$leapYear = false;
if ( ( ( $year % 4 == 0 ) && ( $year % 100 != 0 ) ) || ( $year % 400 == 0 ) )
$leapYear = true;
echo "<p>Did you know that $year is" . ( $leapYear ? "" : " not" ) . " a leap year?</p>";
?>
</body>
</html>
Spoiler: I really hope you knew that this year was a leap year.