Array Error

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
jabustos
Forum Newbie
Posts: 3
Joined: Fri Nov 08, 2002 11:38 am

Array Error

Post by jabustos »

When I write

if ($_GET["id"]) {
echo "Valor de id es " . $_GET["id"];
}
else
echo "no está definido id"
}

If "id" is defined all OK.

But if is not defined:

Undefined index: submit in c:\inetpub\wwwroot\php\ej07.php on line 6

How can I do?
f1nutter
Forum Contributor
Posts: 125
Joined: Wed Jun 05, 2002 12:08 pm
Location: London

Post by f1nutter »

If there are no parameters passed in the address line, then the $_GET array will not contain an index called id or anything else. So the index is undefined.

Try this instead:

Code: Select all

<?php

$id = $_GETї"id"];

if ($id != "")
{ 
 echo "Valor de id es " . $id; 
} 
else
{
 echo "no está definido id" 
}

?>
Also check you brackets
jabustos
Forum Newbie
Posts: 3
Joined: Fri Nov 08, 2002 11:38 am

Post by jabustos »

The code worked in this way:


if (isset($_GET["id"])) {
echo "Valor de id es " . $_GET["id"];
}
else
echo "no está definido id"
}
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If you use empty() instead of isset() then you'll also be able to catch the situation where a user has modified the URL so that id is an empty string or 0:

Code: Select all

if (!empty($_GETї'id'])) { 
    echo 'Valor de id es '. $_GETї'id']; 
} else {
    echo 'no está definido id';
}
Never trust your users :twisted: ... (using trim() on all user input's a good idea too...)

Mac
Post Reply