Undefined Variables = BLEH

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Undefined Variables = BLEH

Post 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?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

first off don't use 'request'

also can you do something like

$var @= "value";?
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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'] : ''; 
?>
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Does this help?

Code: Select all

var $page;
var $query;

$page = $_REQUESTї"page"];
$query = $_REQUESTї"query"];
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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)
Post Reply