Link to home
Start Free TrialLog in
Avatar of mdeek
mdeek

asked on

Getting rid of notices.

I am getting tons of notice errors on my site and would like to get rid of them ofcourse i can do that by setting it so it wont display them but would be better if they were prevented in the first place.

most all of them seem to have the same indication

Notice: Undefined index: usertype in /srv/www/htdocs/agents/public_html/index.php on line 17
 undefined index and always in the session.
Is there a way to set it so it has atleast an empty value in the session so the index atleast exists eventhough its empty ??
sorta like to catch the notice ?



<?php
session_start();
 
include('includes/includes.inc.php');
include('includes/globalvars.php');
 
include('header.inc');
 
 
if (isset($_GET['store'])) {
$storeid = $_GET['store'];
} else {
$storeid = $_SESSION['paywaveid'];
 
 
// line 17 below .. 
if ($_SESSION['usertype'] == 'Employees') {
	$storeid = "";
}
 
}

Open in new window

Avatar of R_Janssen
R_Janssen
Flag of Netherlands image

Choose your style below :-)
// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors
error_reporting(E_ALL);
 
 
You can accomplish this by using isset() to determine if the index is set before attempting to access it:

if (isset($_SESSION['usertype']) &&$_SESSION['usertype'] == 'Employees')
A typo: There should be a space after the '&&'.
if (isset($_SESSION['usertype']) &&$_SESSION['usertype'] == 'Employees')

Open in new window

Avatar of trickyidiot
trickyidiot

better than using isset(), use empty() - this checks for non-existant variables, emtpy values, null values, or values of 0. All of these are technically 'empty'

if (!empty($_SESSION['usertype']) && $_SESSION['usertype'] == 'Employees'){
  // do something here
}
Avatar of mdeek

ASKER

ok but suppose the index doesnt exist so it has not been set , is there a way to give it an empty value ? or a default value so that it does exists and therefor eliminating the notice "undefined index" because then the index exists right ?

for instance array gets pulled index usertype is empty so doesnt exist set the usertype to lets say 1 and later on when needed change that index to something else ?

im just looking for a way to get rid of the notices without resorting to just hiding them with the php.ini configuration.
ASKER CERTIFIED SOLUTION
Avatar of trickyidiot
trickyidiot

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial