Page 1 of 1

Undefined index:

Posted: Sun Nov 11, 2012 10:44 pm
by Fidbeck
I have only this bit of code here

[syntax=php]<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>[/syntax]

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...

Re: Undefined index:

Posted: Mon Nov 12, 2012 12:35 am
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); ?>

Re: Undefined index:

Posted: Mon Nov 12, 2012 9:35 am
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
[syntax=php]$get_album = (isset($_GET['album']));[/syntax]
worked upp until the last echo giving me the message Nothing Found here
the other way was to
[syntax=php]//Get Album
if(isset($_GET['album'])) {
$get_album = $_GET['album'];

//get album code here.
}[/syntax]
EcazS suggestion but right after that it gave me another undefined index error again.
the only way was to silence it

Re: Undefined index:

Posted: Mon Nov 12, 2012 1:37 pm
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.

Re: Undefined index:

Posted: Sat Nov 17, 2012 12:27 am
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
[syntax=php]echo $nothing;[/syntax]
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
[syntax=php]if (isset($nothing)){
echo $nothing;
}[/syntax]
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
[syntax=php]$nothing = 'something';

if (isset($nothing)){
echo $nothing;
}[/syntax]
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
[syntax=php]if (isset($_GET['nothing'])){
$nothing = $_GET['nothing'];
}

echo $nothing;[/syntax]
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
[syntax=php]//Get Album
$get_album = $_GET['album'];[/syntax]
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
[syntax=php]echo $_GET['album'];[/syntax]

Re: Undefined index:

Posted: Sat Nov 17, 2012 9:28 am
by Fidbeck
The only way was to silence it.

I did this [syntax=php]$get_album = $_GET['album'];[/syntax] 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.

Re: Undefined index:

Posted: Tue Nov 20, 2012 12:17 am
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.