Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

register variables=off & expliclty defining varaibles

Status
Not open for further replies.

DaveC426913

Programmer
Joined
Jul 28, 2003
Messages
274
Location
CA
I guess I'm not quite getting this. I'll start from the top.
I'm upgrading PHP code from 4.2 to 4.4. One of the big differences is that register globals defaults to off.

I've temp. switched it to on in php.ini (then restarted Apache) to ensure that the code is running OK, now I'm switching it off again.

I've set a flag on the page:
error_reporting (E_NOTICE);

OK, now I'm getting a notice that tells me it's not defined:

Notice: Undefined index: mes in /usr/local/ on line 78

Here's the code:

<td<?php echo "<a href=\"select_date.php?mes=".($mes-1)."&fm=$fm&fld=$fld\"><<</a>"; ?></center></td>
<td><center>click day to select</center></td>
<td><center><?php echo "<a href=\"select_date.php?mes=".($mes+1)."&fm=$fm&fld=$fld\">>></a>"; ?></center></td>
</tr></table></td>
...
<?php
$newCal->displayMonth($mes);
?>

Good so far. It actually trips over it 3 times, each time it encounters $mes

So, I put in a new line:

$mes=$_GET['mes'];

But it doesn't work. I now that that same Notice on THAT line.


I tried this too:

$mes=$HTTP_GET_VARS['mes'];

No luck. Why am I getting an 'unidefined index: mes' error?
 
i bet they're not the same error message.

the undefined index is that you are not giving the script GET variables so the index "mes" is not existent. the other error will be because you are using an uninstantiated variable.

try this
Code:
if (isset($_GET['mes'])):
  $mes = $_GET['mes'];
else:
  die ("mes variable has not been provided in the query string");
endif;

 
Right. I just checked that.

OK, mes is NOT being set in the query string **and that's OK** (it is normal that it be null first time onto this page).



I guess I thought the purpose of register globals = off would stop $mes from being populated implictly fromt he query string - that I would now have to poulate it explicitly.


Now I'm confuseder - I'm no longer sure *what* changes I'm supposed to be making to ensure this pre-4.4 code is compatible on our 4.4 system.
 
it is not being populated implicitly. but your script refers to is as you are not conditionalising the relevant code:

Code:
if (isset($_GET['mes'])):
 //value is in
 $mes= $_GET['mes'];
 //echo some code;
else:
  // echo some other code
endif;

alternatively if you want $mes to default to a particular content in the absence of an explicitly provided variable

Code:
$default = ""; //default value for $mes
$mes = isset($_GET['mes']) ? $_GET['mes'] : $default;
 
But why jump through all those hoops? What's wrong with simply referencing $_GET['mes'] where every you need to?

After all, six months after you have written a complex script and are debugging a logic fault in line 600, you may not remember that $mes got its value in line 23. But if you see a references to $_GET['mes'] on line 599, you know the source of that value.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top