Tuesday, 20 March 2018

PHP Undefined Index




I have this code and it's giving me an undefined error if country is not a variable in the URL. I've never had this problem before so it's obviously something to do with server settings but I'd rather not change those because this is probably done for security purposes. I've searched Google and can't find the workaround, although I'm sure it's blindingly obvious!



$country = $_GET['country'];

if(!$country){
$incoming = $outgoing = $sms = $free = "---";
} else {
get_rates($country);
}

Answer



you should use the following approach:



if (!isset($_GET['country'])) {
$incoming = $outgoing = $sms = $free = "---";
} else {
get_rates($_GET['country']);
}

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...