Page 1 of 1

Undefined Variables = BLEH

Posted: Fri Aug 30, 2002 5:36 pm
by JPlush76
I changed my php.ini file to display all errors

Basically I'm making a page that displays results 20 at a time and its working fine, however I keep getting UNDEFINED VARIABLES, UNDEFINED INDEX because when they first call the page these vars don't exist.

When I load it up to my server it looks fine because they have the simple errors turned off, but on my local machine it bugs me.

The easy thing to do is to lower my level checking in php.ini BUT is that the best thing? Shouldn't I write scripts with 0 errors at all or is this part of what we have to do?

I could also write a ton of if/else statements to check for values, is that the only way to have error free coding?

GIVES ERRORS:

Code: Select all

<?php
$page = $_REQUESTї'page'];
$query = $_POSTї'query'];
?>

DOESNT GIVE ERRORS

Code: Select all

<?php
if(isset($_REQUESTї'page']))
{
	$page = $_REQUESTї'page'];
}
if(isset($_POSTї'query']))
{
	$query = $_POSTї'query'];
}	
?>

what do you guys usally do for your variables?

Posted: Fri Aug 30, 2002 8:35 pm
by hob_goblin
first off don't use 'request'

also can you do something like

$var @= "value";?

Posted: Fri Aug 30, 2002 9:03 pm
by JPlush76
yea I could always supress the variable too, but its still an error

I was just seeing if its just good practice to always define your varariables so your scripts are as error free as possible no matter how small the error

Posted: Fri Aug 30, 2002 10:07 pm
by volka

Code: Select all

<?php 
$page = $_REQUESTї'page']; 
$query = $_POSTї'query']; 
?>
those values usally have a purpose. So I think a check is always recommend. Normally I assign default values if they are not mandatory.

Code: Select all

<?php 
$page = (isset($_REQUESTї'page']))? $_REQUESTї'page'] : 'index.html'; 
$query = (isset($_POSTї'query'])) ? $_POSTї'query'] : ''; 
?>

Posted: Sat Aug 31, 2002 8:50 am
by Takuma
Does this help?

Code: Select all

var $page;
var $query;

$page = $_REQUESTї"page"];
$query = $_REQUESTї"query"];

Posted: Sat Aug 31, 2002 9:11 am
by volka
PHP Parse error: parse error, unexpected T_VAR in xyz.php on line 2
$page and $query are not the problem. When the page is called the first time, there are no entries for 'page' or 'query' in the _REQUEST-array (nothing has been transmitted yet)