In a php page I have following code:
if($_REQUEST['c']!="") // I get error on this line itself. Why? { $pidis=(int)($_REQUEST['c']); }
I keep getting Undefined index error.
On Googling I manage to understand that if a page is access without parameters (in URL) which we are trying to access we can get this error/warning. I believe that if a parameter is not defined in the URL it should just return empty instead of giving error/warning message.
I know that it is possible to suppress errors and warning by adding
error_reporting(E_ALL ^ E_NOTICE);
But I do not want to do this.
This same page work just fine on our company’s web server but does not work on our clients web server.
Why is this happening?
How to solve this problem?
Advertisement
Answer
You are getting that error because you are attempting to compare $_REQUEST['c']
to something when $_REQUEST['c']
does not exist.
The solution is to use isset() before comparing it. This will remove the warning, since the comparison won’t happen if $_REQUEST['c']
doesn’t exist.
if(isset($_REQUEST['c']) && $_REQUEST['c']!="") { $pidis=(int)($_REQUEST['c']); }
It is an E_NOTICE
level error, and your level of error reporting will affect whether the error shows up or not. Your client’s server has E_NOTICE
level error reporting turned on, which is why it shows up there.
It is a good idea to always develop using E_ALL
so that you can catch this kind of error before moving your code to other servers.