How to fix Undefined index warning in php - php

I get Notice: Undefined index: sesStatus warning and I am able to fix this warning when its just a variable, but this is part of a code and I am not sure how to fix the warning.
Her is the code that its doing it on.
case $_SESSION['sesStatus']=='e_logged':
$cc_section = $cc_emp;
$sidecol = $sc_employer;
break;

Replace case $_SESSION['sesStatus']=='e_logged':
with
case isset($_SESSION['sesStatus']) && $_SESSION['sesStatus']=='e_logged':

Assuming all else is well, I'd start by testing for the existence of the key with isset(). Also, testing for equality in the case statement is unusual.
<?php
if(isset($_SESSION['sesStatus'])) {
switch($_SESSION['sesStatus']) {
case 'e_logged':
$cc_section = $cc_emp;
$sidecol = $sc_employer;
break;
}
}

Related

PHP is_numeric error handling if value not set

I have the following code in PHP
if (is_numeric($args['myargs']['custom_value'])) {
echo 'Yes';
} else {
echo 'No';
}
It runs correctly, but if custom_value is not set then I get the warning in my logs..
PHP Notice: Undefined index: custom_value
I think this is just a notice and not an error so can be safely ignored? Is it bad practice to do it like this?
to avoid the warning you should do something like this
if(isset($args['myargs']['custom_value'])) {
if (is_numeric($args['myargs']['custom_value'])) {
echo 'Yes';
} else {
echo 'No';
}
}
What's happening
PHP sees you are trying to use an array element that is not set, so it helpfully warns you about it. It's not serious in this case, but you want to learn to avoid the messages.
The solution
The function isset will test if the array key is defined.
//You must first of all test isset and then is_numeric,
// else you still get the error. Research 'short circuiting' in php
if ( isset($args['myargs']['custom_value']) && is_numeric($args['myargs']['custom_value'])) {
echo 'Yes';
} else {
echo 'No';
}
This solution will also print "No" if the array key was never defined.
alse you can
error_reporting(0)
in php file beginning

Identical statement condition

Correct me if I'm wrong. I saw many people write condition header as this format.
if(isset($var) && $var!=""){}
Basically it just checking if $var isn't null value right? In my understanding this would be enough.
if($var){}
Am I missing something? Did my method unreliable? My colleague already pointed out that I should use the first one. Sometime I just lazy to write so I just want to make it simple.
They are not the same thing.
if (expr) is evaluated with these rules -> http://php.net/manual/en/types.comparisons.php#types.comparisons
<?php
$var = "0";
if (isset($var) && $var != "") {
echo "first block\n";
}
if ($var) {
echo "second block\n";
}
Example
Suppose $var is "0", then it is definitely isset, and not equal to an empty string, but fails the if ($var) test.
php
if($var){}
will check if $var is true. In this scenario, if $var is used for first time in condition, notice will be generated that you are trying to use undefined variable.
The isset () function is used to check whether a variable is set or not. If variable is not set, possibly it will enter to the else condition.
In your case if $var is not available, Possibly you will hit by this error Undefined variable: var. So checking isset is always good for your code.
Case 1:
if($var){
echo 'pass';
}else{
echo 'fail';
}
Output:Notice: Undefined variable: var in E:\siva\htdocs\test.php on line 48
fail
Case 2:
if(isset($var)){
echo 'pass';
}else{
echo 'fail';
}
Output : fail
Case 3:
$var = '';
if($var){
echo 'pass';
}else{
echo 'fail';
}
Output :Fail
In this case 3 is more interesting, If you failed to use isset, Defined the variable as null, So you will not get the error

PHP Undefined Index - using isset() switch statment [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 9 years ago.
I understand there are many posts on this issue. I have looked at the many many posts but for some reason I can seem to find an answer!
Any help will be very much appreciated. I am fairly new to PHP so I apologise if I say anything incorrectly.
I am trying to create a basic basket system using an array. I keep getting an error undefined index on line $_SESSION['cart'][$app_ID]++; The funny thing is it all functions correctly! I want to solve the error and not just turn off the error reporting.
if(isset($_GET['id'])){
$app_ID = $_GET['id']; //the item id from the URL
$action = $_GET['action']; //the action from the URL
$total = 0;
if (isset($app_ID)){
switch($action) {
case "add":
$_SESSION['cart'][$app_ID]++;
break;
case "remove":
$_SESSION['cart'][$app_ID]--;
if($_SESSION['cart'][$app_ID] == 0) unset($_SESSION['cart'][$app_ID]);
break;
case "empty":
unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart.
break;
Thanks guys and gals.
You should use isset($_SESSION['cart'][$app_ID]) and maybe isset( $_SESSION['cart']) before, everywhere.
Generally you must make sure that and array index is present before referencing it. You can do this either with isset(), or writing code where that condition is inevitable (e.g. adding the index somewhere earlier).
The other part of your question, I suppose, is why your code works. The explanation is easy. When you reference a non-existent index, the notice you observed is emitted (in non-production environments) but that does not stop the program. Since there is nothing to be used, null is returned for that array value. So the value is assumed to be null, and ++ takes values, as integers, and null is converted to the integer 0, and then raised by one. Since ++ is an operator that writes, it will create the array item for you. Since $a++ is defined as $a=$a+1 it's easy to see that what you've written is $_SESSION['cart'][$app_ID]=$_SESSION['cart'][$app_ID]+1 which is in turn $_SESSION['cart'][$app_ID]=null+1 where null+1 is executed as 0+1 yielding 0, so 0 is assigned to (the formerly missing) array item. Hope this helps see clear. ;)
to use $_SESSION you must call session_start () first before send any header information
I hope this help you,
Cheers,
session_start(); // at the top
case "add":
if (isset( $_SESSION['cart'][$app_ID] )){
$_SESSION['cart'][$app_ID]++;
} else {
$_SESSION['cart'][$app_ID] = 1;
}
break;
It's worth to mention, that it's merely a Notice, not an error. You basically must check for array index existence and initialize it before referencing it.
if (isset($app_ID)) {
switch($action) {
case "add":
if (!isset($_SESSION['cart']) {
$_SESSION['cart'] = array();
}
if (!isset($_SESSION['cart'][$app_ID]) {
$_SESSION['cart'][$app_ID] = 0;
}
$_SESSION['cart'][$app_ID]++;
break;
case "remove":
if (isset($_SESSION['cart'] && isset($_SESSION['cart'][$app_ID]) {
$_SESSION['cart'][$app_ID]--;
if ($_SESSION['cart'][$app_ID] <= 0) {
unset($_SESSION['cart'][$app_ID]);
}
}
break;
case "empty":
unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart.
break;
}
}
I've also changed == 0 in remove to <= 0 just to be safe.

php $_GET and undefined index

A new problem has arisen for me as I tried to run my script on a different PHP Server.
ON my old server the following code appears to work fine - even when no s parameter is declared.
<?php
if ($_GET['s'] == 'jwshxnsyllabus')
echo "<body onload=\"loadSyllabi('syllabus', '../syllabi/jwshxnporsyllabus.xml', '../bibliographies/jwshxnbibliography_')\">";
if ($_GET['s'] == 'aquinas')
echo "<body onload=\"loadSyllabi('syllabus', '../syllabi/AquinasSyllabus.xml')\">";
if ($_GET['s'] == 'POP2')
echo "<body onload=\"loadSyllabi('POP2')\">";
elseif ($_GET['s'] == null)
echo "<body>"
?>
But now, on a my local server on my local machine (XAMPP - Apache) I get the following error when no value for s is defined.
Notice: Undefined index: s in C:\xampp\htdocs\teaching\index.php on line 43
Notice: Undefined index: s in C:\xampp\htdocs\teaching\index.php on line 45
Notice: Undefined index: s in C:\xampp\htdocs\teaching\index.php on line 47
Notice: Undefined index: s in C:\xampp\htdocs\teaching\index.php on line 49
What I want to happen for the script to call certain javascript functions if a value is declared for s, but if nothing is declared i would like the page to load normally.
Can you help me?
Error reporting will have not included notices on the previous server which is why you haven't seen the errors.
You should be checking whether the index s actually exists in the $_GET array before attempting to use it.
Something like this would be suffice:
if (isset($_GET['s'])) {
if ($_GET['s'] == 'jwshxnsyllabus')
echo "<body onload=\"loadSyllabi('syllabus', '../syllabi/jwshxnporsyllabus.xml', '../bibliographies/jwshxnbibliography_')\">";
else if ($_GET['s'] == 'aquinas')
echo "<body onload=\"loadSyllabi('syllabus', '../syllabi/AquinasSyllabus.xml')\">";
else if ($_GET['s'] == 'POP2')
echo "<body onload=\"loadSyllabi('POP2')\">";
} else {
echo "<body>";
}
It may be beneficial (if you plan on adding more cases) to use a switch statement to make your code more readable.
switch ((isset($_GET['s']) ? $_GET['s'] : '')) {
case 'jwshxnsyllabus':
echo "<body onload=\"loadSyllabi('syllabus', '../syllabi/jwshxnporsyllabus.xml', '../bibliographies/jwshxnbibliography_')\">";
break;
case 'aquinas':
echo "<body onload=\"loadSyllabi('syllabus', '../syllabi/AquinasSyllabus.xml')\">";
break;
case 'POP2':
echo "<body onload=\"loadSyllabi('POP2')\">";
break;
default:
echo "<body>";
break;
}
EDIT: BTW, the first set of code I wrote mimics what yours is meant to do in it's entirety. Is the expected outcome of an unexpected value in ?s= meant to output no <body> tag or was this an oversight? Note that the switch will fix this by always defaulting to <body>.
Get into the habit of checking if a variable is available with isset, e.g.
if (isset($_GET['s']))
{
//do stuff that requires 's'
}
else
{
//do stuff that doesn't need 's'
}
You could disable notice reporting, but dealing them is good hygiene, and can allow you to spot problems you might otherwise miss.
I always use a utility function/class for reading from the $_GET and $_POST arrays to avoid having to always check the index exists... Something like this will do the trick.
class Input {
function get($name) {
return isset($_GET[$name]) ? $_GET[$name] : null;
}
function post($name) {
return isset($_POST[$name]) ? $_POST[$name] : null;
}
function get_post($name) {
return $this->get($name) ? $this->get($name) : $this->post($name);
}
}
$input = new Input;
$page = $input->get_post('page');
I was having the same problem in localhost with xampp. Now I'm using this combination of parameters:
// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);
php.net: http://php.net/manual/pt_BR/function.error-reporting.php
First check the $_GET['s'] is set or not. Change your conditions like this
<?php
if (isset($_GET['s']) && $_GET['s'] == 'jwshxnsyllabus')
echo "<body onload=\"loadSyllabi('syllabus', '../syllabi/jwshxnporsyllabus.xml', '../bibliographies/jwshxnbibliography_')\">";
elseif (isset($_GET['s']) && $_GET['s'] == 'aquinas')
echo "<body onload=\"loadSyllabi('syllabus', '../syllabi/AquinasSyllabus.xml')\">";
elseif (isset($_GET['s']) && $_GET['s'] == 'POP2')
echo "<body onload=\"loadSyllabi('POP2')\">";
elseif (isset($_GET['s']) && $_GET['s'] == null)
echo "<body>"
?>
And also handle properly your ifelse conditions
I recommend you check your arrays before you blindly access them :
if(isset($_GET['s'])){
if ($_GET['s'] == 'jwshxnsyllabus')
/* your code here*/
}
Another (quick) fix is to disable the error reporting by writing this on the top of the script :
error_reporting(0);
In your case, it is very probable that your other server had the error reporting configuration in php.ini set to 0 as default.
By calling the error_reporting with 0 as parameter, you are turning off all notices/warnings and errors. For more details check the php manual.
Remeber that this is a quick fix and it's highly recommended to avoid errors rather than ignore them.
You should check wheter the index exists before use it (compare it)
if (isset($_GET['s']) AND $_GET['s'] == 'foobar') {
echo "foo";
}
Use E_ALL | E_STRICT while developing!
Actually none of the proposed answers, although a good practice, would remove the warning.
For the sake of correctness, I'd do the following:
function getParameter($param, $defaultValue) {
if (array_key_exists($param, $_GET)) {
$value=$_GET[$param];
return isSet($value)?$value:$defaultValue;
}
return $defaultValue;
}
This way, I check the _GET array for the key to exist without triggering the Warning. It's not a good idea to disable the warnings because a lot of times they are at least interesting to take a look.
To use the function you just do:
$myvar = getParameter("getparamer", "defaultValue")
so if the parameter exists, you get the value, and if it doesnt, you get the defaultValue.
Avoid if, else and elseifs!
$loadMethod = "";
if(isset($_GET['s'])){
switch($_GET['s']){
case 'jwshxnsyllabus':
$loadMethod = "loadSyllabi('syllabus', '../syllabi/jwshxnporsyllabus.xml', '../bibliographies/jwshxnbibliography_')";
break;
case 'aquinas':
$loadMethod = "loadSyllabi('syllabus', '../syllabi/AquinasSyllabus.xml')";
break;
case 'POP2':
$loadMethod = "loadSyllabi('POP2')";
}
}
echo '<body onload="'.$loadMethod.'">';
clean, readable code is maintainable code
Simple function, works with GET or POST. Plus you can assign a default value.
function GetPost($var,$default='') {
return isset($_GET[$var]) ? $_GET[$var] : (isset($_POST[$var]) ? $_POST[$var] : $default);
}
Another option would be to suppress the PHP undefined index notice with the # symbol in front of the GET variable like so:
$s = #$_GET['s'];
This will disable the notice. It is better to check if the variable has been set and act accordingly.
But this also works.
The real answer to this is to put a # At symbol before the variable which will suppress the error
#$_GET["field"]
#$_POST["field"]
It will work some slower, but will keep the code clean.
When something saves time for the programmer, and costs time for the website users (or requires more hardware), it depends on how much people will use it.

Yet Another Notice: Undefined index Question

I keep getting
Notice: Undefined index: action
When I use the following code. I use it to see which page is required. Anyway to sort this out? I know you're not supposed to just include files from user input (without checking the input first), but this switch statement only works if action is set to view or blah, otherwise it just shows the main page.
?action=view or ?action=blah
switch ($_GET['action'])
{
case 'view':
echo "We are in view";
require FORUM_ROOT . 'view2.php';
break;
case 'blah':
echo "We are in blah";
break;
default:
"This is default";
require FORUM_ROOT . 'main.php';
}
Rewrite your code in this way:
$action = isset($_GET['action']) ? $_GET['action'] : null;
switch ($action) { ... }
The error means that you are attempting to use $_GET['action'] when it doesn't exist. Like for instance, when you go to the page without passing page.html?action=xxx
You're using an array element without checking whether it exists. You should make sure that your code does not try to read $_GET['action'] when that might not be defined.
You can do this in a way that doesn't require altering your switch logic, by giving $_GET['action'] a defined (but "empty") value if action wasn't given in the query string:
if (!isset($_GET['action']))
$_GET['action'] = null;
switch ($_GET['action']) {
...
}
There is a specific syntax construct # for ignoring notices when they are provably redundant.
switch (#$_GET['action']) {
The isset ternary is used as microoptimization workaround, but otherwise has no practical benefit in your case.

Categories