Solved

php undeclared variable behavior differences between IIS and Apache

Posted on 2011-03-01
5
464 Views
Last Modified: 2012-05-11
We have been recently switching over from apache to IIS and we are having problems with undeclared variables.

stuff like  
<?php
 //$variable not declared yet;
 if($variable != 1) 'echo something';
?>

Open in new window


issues a notice but also doesn't run whats in the if statement.  In the previous environment this would still run.  I'm blaming this on IIS because i beleive the php versions are close if not the same.

I realize this is not exactly best practice but it would not be worth the effort to change all of our old code.  Is there something in the php ini file i can change that would make it handle undeclared variables differently?
0
Comment
Question by:brad0525
5 Comments
 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 150 total points
ID: 35007442
Why not using this (from an example:
http://www.php.net/manual/en/function.error-reporting.php
0
 
LVL 13

Expert Comment

by:darren-w-
ID: 35007569
Hi,

You need to specify in you php.ini file that the system should not report warnings, the version of php points to may differ to the apache version hence the system is reporting the errors.

so display_errors = Off
on the live environment.

D
0
 
LVL 7

Expert Comment

by:Vimal DM
ID: 35008932
Hai,

 - What ever the version that you follow in PHP

 - it is perfectly, that the variable should have the at least the empty value

 - only than you can use that particular variable for any process

0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 350 total points
ID: 35011349
The default PHP installation is usually

error_reporting(E_ALL ^ E_NOTICE);

Try adding that to the top of your script.   It will let you use undefined variables as if they were NULL, FALSE or zero, depending on the loose typecasting that PHP provides.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 35011368
And a sidebar note - you almost always want to do your development with error_reporting(E_ALL).  That way when you have a typographical error and you accidentally use an undefined variable, you will get a Notice instead of a silent substitution that is much harder to debug!

Best of luck with it, ~Ray
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question