Undefined index:

Ask about a PHP problem here.
Post Reply
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Undefined index:

Post by Fidbeck »

I have only this bit of code here
<html>
<head>

</head>
<body>

<?php
$page = $_SERVER['PHP_SELF'];

//Settings
$column = 5;

//Directories
$base = "data";
$thumbs = "thumbs";

//Get Album
$get_album = $_GET['album'];
?>
</body>
</html>
and its displaying me this error
Notice: Undefined index: album in C:\xampp\htdocs\photo\album.php on line 18

I don't know why...
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Undefined index:

Post by Temor »

That is not really an error.
PHP gives you that notice because it doesn't know that $_GET['album'] is set.

You can disable it by setting error reporting to E_ALL &~ E_NOTICE.
Put this at the top of the page.
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Undefined index:

Post by Fidbeck »

I didn't solve that way, because I only say this today but it's already solved.

I tried with 3 different ways and It didn't work
One was like this
$get_album = (isset($_GET['album']));
worked upp until the last echo giving me the message Nothing Found here
the other way was to
//Get Album
if(isset($_GET['album'])) {
    $get_album = $_GET['album'];
    
    //get album code here.
}
EcazS suggestion but right after that it gave me another undefined index error again.
the only way was to silence it
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Undefined index:

Post by Temor »

Well, as I said, it's not an error. It's a Notice. It's notifying you that something could potentially be wrong, not that it is wrong.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Undefined index:

Post by jacek »

You shouldn't really just hide these since they often point to more serious problems. If you understand the problem it is pretty easy to avoid.

As already said above you get the error when you try to use a variable that does not exist, like
echo $nothing;
will print an undefined variable message. You can check to see if a variable is defined using isset(), so this can use used to make sure things are defined before you use them
if (isset($nothing)){
    echo $nothing;
}
this would not cause the error. Obviously if you are sure something is defined there is no point checking it so don't do things like this
$nothing = 'something';

if (isset($nothing)){
    echo $nothing;
}
It can get a bit more complicated when you assign a variable in a block for example if you assign $_GET['nothing'] to $nothing only if it's set
if (isset($_GET['nothing'])){
    $nothing = $_GET['nothing'];
}

echo $nothing;
will still cause the undefined variable error if the $_GET variable is not set. In this situation it would be better to just use $_GET directly inside the check.

A final point, where you have
//Get Album
$get_album = $_GET['album'];
You are not getting the album, you already have the album, it's stored in $_GET['album'] which you can use like any other variable
echo $_GET['album'];
Image
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Undefined index:

Post by Fidbeck »

The only way was to silence it.

I did this
$get_album = $_GET['album'];
because is faster to type $get_album than $_GET['album']
but i'm just assigning something to a variable, so it shouldn't give an error.

and again even though I got that error the code worked like it was supposed to.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Undefined index:

Post by jacek »

Fidbeck wrote:The only way was to silence it.
That's not true, there is also the method I described above of making sure not to use a variable that has no value.
Fidbeck wrote:but i'm just assigning something to a variable, so it shouldn't give an error.
It should if the variable you are assigning ($_GET['album_id'] in this case) has no value.
Fidbeck wrote:and again even though I got that error the code worked like it was supposed to.
and again it might in this case but that is not always true.
Image
Post Reply