Notice: How to clean undefined variables?

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
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Notice: How to clean undefined variables?

Post by bob_the _builder »

Hi,

How do u clear errors like:

Code: Select all

if($photos_uploaded['size'][$counter] > 0)
{
	if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
{
	echo 'File '.($counter+1).' is not a photo!<br />';
When its false I get error: Notice: Undefined offset:

and

Code: Select all

if($_GET['page'] == NULL) { 
    	$page = 1; 
}else{ 
    	$page = $_GET['page']; 
}
gives error: Notice: Undefined index

when get page is not null

?

Thanks
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

Hi,

I guess you mean change it to:

Code: Select all

if (isset($_GET['page'])) {
		$page = $_GET['page'];
}else{
		$page = 1; 
}
What about code like:

Code: Select all

if($photos_uploaded['size'][$counter] > 0)
Your not really looking for isset?


Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

But you can first check the existence and then perform the comparison

Code: Select all

if( isset($photos_uploaded['size'][$counter]) && $photos_uploaded['size'][$counter]>0 )
for this to work without warnings it doesn't matter wether $photos_uploaded does not exist or has no element size or [$counter]. It even works if $photos_uploaded is an integer (not an array). But $counter has to exist or you will get a warning.
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

Hi,

I tried that before I posted back, got an error:

Parse error: parse error, unexpected '>', expecting ',' or ')'


Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Check your code again.
No syntax error in

Code: Select all

<?php
$counter = 0;
if( isset($photos_uploaded['size'][$counter]) && $photos_uploaded['size'][$counter]>0 )
{
	exit();
}
?>
Post Reply