views:

232

answers:

3

I am running IIS7 and PHP5 locally on my machine and the code above will cause IIS to throw an HTTP 500 error. After a lot of frustration and confusion I figured out that any PHP script with empty variable will throw this IIS error. Below is an example of this:

<?php $x = $y; ?>

I find this behavior very odd and it breaks a large percentage of the code I write. In most cases it prevents me from debugging properly. I would really really like to see output and error messages. Also, this can break the usage of the super globals GET and POST and can only be suppressed by logic to prevent their execution.

How can I fix this?

+1  A: 

PHP is throwing an exception, printing to stdout, and reporting the error properly in all likelihood. IIS is probably ignoring that and throwing the 500.

Try logging your errors to file instead of displaying them, which may appease IIS. I believe it's:

display_errors = Off
log_errors = On
error_log = "C:/YourWWW/errors.txt"

...but I don't have a php.ini handy to check.

Back to the issue, if $y is empty, what do you expect PHP to do? Any sane programming language will vomit when you try to assign a non-existent variable. This will sound harsh, but if that "breaks the majority of code you write", you're doing something wrong. Also, not sure what you mean by "break the usage of" $_GET and $_POST. If you mean this will throw a 500:

$foo = $_GET["nonexistentvar"];

...then you're quite right it will. You should be checking for the existence of the key before expecting PHP to assign something that doesn't exist. Pow, no 500:

$foo = isset($_GET["nonexistentvar"]) ? $_GET["nonexistentvar"] : NULL;

Sorry if I sound hostile, but you came across really whiny in your question.

Jed Smith
Or for brevity, and depending on code style:$foo = isset($_GET['nonexistentvar']) ? $_GET['nonexistentvar'] : NULL;$foo = !empty($_GET['nonexistentvar']) ? $_GET['nonexistentvar'] : NULL;First one for checking that foo is set (but could be empty or a value that evaluates to empty), second one for implicitly checking that it's set and also set to something that evaluates as non-empty.All of these approaches will work and are much better than not handling for empty/lowering PHP's error level.
Ben
It does sound hostile, but I don't hate you for it, it is understandable. I am sure it is not because PHP is throwing an exception because the 500 is not thrown in any other situation. Also I should note that when I write code when I am just testing ideas I don't try to add complicated validation and other such crap because it is not production code. I'm not doing anything wrong. Sorry if I confused you because you sound really smart. This prototyping process worked fine under Apache. Sorry if I sound hostile, but you blindly attacked my ego when you answered my question.
teh_noob
@Ben: I agree, I threw array_key_exists in there because it's been a long, long time since I touched PHP and I couldn't remember isset(). I've updated the answer accordingly, thanks. :)
Jed Smith
@teh_noob: No attack meant, just giving you some tough love. One-off for testing is understandable; however, I take the practice (and this is a good one) that all code is production code. You're less likely to slip into bad habits if you treat every line of code like it'll be reviewed by someone else.
Jed Smith
+1  A: 

I use IIS7 + PHP setup using the fascgi module and do not get this issue. PHP outputs errors properly. I would verify that you have PHP and IIS set up properly according to this guide:

http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/

The most common pitfall is that you assigned IIS to call php-win.exe or php.exe vs calling php-cgi.exe. Also, some of the php.ini settings can cause this problem. I cannot recall which however.

Kevin Peno
A: 

Hi teh_noob, I am facing the same problem as you had. Could you advise me how did you fix your problem? Is there any way to config the IIS or php.ini to avoid this problem? Thanks.