Re: Undefined index error
| From: | Daniel Brown | Date: | Mon, 19 Jan 2009 20:28:52 +0000 |
| Subject: | Re: Undefined index error | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-286708@lists.php.net to get a copy of this message | ||
On Mon, Jan 19, 2009 at 14:32, Richard Heyes <richard@php.net> wrote:
>> Notice: Undefined index: 2 in /home/raosetc/public_html/purl/p.php on line
>> 19
>>
>> and here is Line 19:
>>
>> $data = explode("/", $_SERVER['REQUEST_URI']);
When I view that file, on line 19, I see:
echo <<<HTML
> Are you sure it's an array? Use print_r() or var_dump() on
> $_SERVER['REQUEST_URI'] on the preceding line. Just for testing, you
> could do this too:
>
> $_SERVER['REQUEST_URI'] = 'It/Works';
>
> ...on the preceding line.
Well, that wouldn't address the issue of the code he gave on the
line reported, but I know what you mean, Richy. He's turning
$_SERVER['REQUEST_URI'] into an array with explode() if it contains a
forward-slash ("/").
What would be better in that case would be:
<?php
$data = isset($_SERVER['REQUEST_URI']) &&
strlen($_SERVER['REQUEST_URI']) > 0 ?
explode('/',$_SERVER['REQUEST_URI']) : $_SERVER['REQUEST_URI'];
if(is_array($data)) {
// Handle as appropriate.
}
?>
However, that's not the best option as:
1.) REQUEST_URI may not (though it should) have *any* forward
slashes in the URI passed to PHP by the HTTP server.
2.) It leaves too much other stuff to be done.
Instead, check out:
<?php
$data = basename($_SERVER['REQUEST_URI']);
echo $data; // In case you're not yet aware of what you'll receive
from the use of basename()
?>
--
</Daniel P. Brown>
daniel.brown@parasane.net || danbrown@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Unadvertised dedicated server deals, too low to print - email me to find out!