Page 1 of 1

Array Error

Posted: Fri Nov 08, 2002 11:38 am
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?

Posted: Fri Nov 08, 2002 11:49 am
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

Posted: Fri Nov 08, 2002 12:11 pm
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"
}

Posted: Mon Nov 11, 2002 2:34 am
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