Undefined index: Error in php script

Another solution is to use the following:

$pidis = isset($_REQUEST['c']) ? $_REQUEST['c'] : '';

You can also, if you prefer to return a value other than empty, by placing a default value within the final set of single quotes, e.g.

$pidis = isset($_REQUEST['c']) ? $_REQUEST['c'] : 'Default Value';

or return a different variable type, for instance an integer:

$pidis = isset($_REQUEST['c']) ? $_REQUEST['c'] : 34;

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.


Instead of isset() you can also use: array_key_exists().

The difference between both methods is that isset() checks also whether the value of the variable is null. If it is null then isset returns false whereas array_key_exists() returns always true if the key exists (no mater which value). E.g.:

$array = array('c' => null);

var_dump(isset($array['c']))); // isset() returns FALSE here
var_dump(array_key_exists($array['c']); //array_key_exists() returns TRUE

Depending on the context, it is important to distinguish this. In your case I don't think it matters doesn't matter, as (I guess) a request parameter never will be null (except one overwrites it manually).

Tags:

Php

Request