Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php $_GET and undefined index

Tags:

php

undefined

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?

like image 708
Jeff Avatar asked Oct 24 '11 14:10

Jeff


People also ask

How do I fix Undefined index in PHP?

To resolve undefined index error, we make use of a function called isset() function in PHP. To ignore the undefined index error, we update the option error_reporting to ~E_NOTICE to disable the notice reporting.

What causes Undefined index in PHP?

Notice Undefined Index in PHP is an error which occurs when we try to access the value or variable which does not even exist in reality. Undefined Index is the usual error that comes up when we try to access the variable which does not persist.

How do I get rid of notice Undefined index?

You can get rid of the PHP undefined index notice by using the isset() function. The PHP undefined variable notice can be removed by either using the isset() function or setting the variable value as an empty string.


1 Answers

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>.

like image 161
Rudi Visser Avatar answered Sep 29 '22 00:09

Rudi Visser