Book Example Throws a 500 error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cSheffield90
Forum Newbie
Posts: 4
Joined: Mon Jul 11, 2016 6:46 pm

Book Example Throws a 500 error

Post by cSheffield90 »

Hello everyone. Im trying to learn php so I picked up the book "Beginning php 5.3"

Right now I am using SSH to learn on a rpi2. I follow along and write the code in notepad++ on my Win10 machine. I SFTP the file over and use SSH to move the files to my /var/www/html directory. I then access the php file on my Win10 machine.
rpi2 is ...

PHP Version 5.6.22-0+deb8u1
System Linux raspberrypi 4.4.13-v7+ #894 SMP Mon Jun 13 13:13:27 BST 2016 armv7l
Build Date Jun 14 2016 17:21:33
Server API Apache 2.0 Handler

So far I got to chapter 4 without any problems. Once I got to this example I kept getting 500 errors in my browser.
What I ended up doing is commenting out sections to isolate and at the very least the first if statement is throwing one.

Does anyone have any common sense they can slap into me as to why this might happen?

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>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Book Example Throws a 500 error

Post by requinix »

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

Code: Select all

if ( $hour => 0 && $hour < 12 )
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.
cSheffield90
Forum Newbie
Posts: 4
Joined: Mon Jul 11, 2016 6:46 pm

Re: Book Example Throws a 500 error

Post by cSheffield90 »

Im not 100 percent sure but the formatting issues might have been caused by my copy/pasting from the pdf into notepad++ (or i might have pasted into a text file on the Pi and saved as php). At the start I was hand typing it myself but as the examples grew longer I ended up getting lazy and I copy/pasted that last example.

After a while I just opened a text editor over SSH and hand typed it all out and it ran.

Thanks for the heads up on getting a more recent book. I knew not to delve into php6 so i figured any iteration of 5 would be just fine.
Thanks so much for the great detailed explanation covering the error check enabling. The book never covered that which I wish it had.
Also thanks for explaining the non breaking spaces.

ps...yes I remember it was a leap year. After hand typing the code i changed the boolean variable name when declaring it but forgot to change the rest of them. It showed up this year wasn't a leap year and I had a head scratcher for a second haha.
Last edited by cSheffield90 on Mon Jul 11, 2016 11:34 pm, edited 1 time in total.
cSheffield90
Forum Newbie
Posts: 4
Joined: Mon Jul 11, 2016 6:46 pm

Re: Book Example Throws a 500 error

Post by cSheffield90 »

omgosh.

Thank you so much again for the error output instructions. That is so much nicer! Ive programmed in C for the longest time and switching over to php and thinking I wouldnt get any detailed error messages scared me a bit. Im infamous for missing the occasional semicolon.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Book Example Throws a 500 error

Post by requinix »

cSheffield90 wrote:Im not 100 percent sure but the formatting issues might have been caused by my copy/pasting from the pdf into notepad++
That could totally do it.
cSheffield90 wrote:I knew not to delve into php6 so i figured any iteration of 5 would be just fine.
PHP 5.x was a bit weird. 5.0 was fairly normal with some big sweeping changes from 4.x, and 5.1 and 5.2 were fairly normal with lesser changes, but they didn't last very long (relatively) until 5.3 came out. That version started a trend of making much more significant changes in "minor" revisions, which has continued through 5.4, 5.5, and 5.6. Then the devs finally decided to move up to a new major version, which was obviously 7. Because ~reasons~.

To the point, PHP has an official list of end-of-life and current versions of PHP. 5.5 actually just dropped out of support, so only 5.6 and 7.0 are even supported anymore (and 7.1 will probably hit beta soon).
Post Reply