/    Sign up×
Community /Pin to ProfileBookmark

Undefined Variable Error For Nothing

Folks,

I have defined the $country but I get error I have not.
Why ?

[code]
<?php
$two_letter_country_code=iptocountry(“101.102.103.104”);

function iptocountry($ip) {
$numbers = preg_split( “/./”, $ip);
include(“ip_files/”.$numbers[0].”.php”);
$code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);
foreach($ranges as $key => $value){
if($key<=$code){
if($ranges[$key][0]>=$code){$country=$ranges[$key][1];break;}
}
}
if ($country==””){$country=”unkown”;}
return $country;
}
?>

<?php
echo “$country”;
?>
[/code]

Error on this line: echo “$country”;

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 05.2019 — So...if that if condition where you assign a value to $country is never matched, then $country is never defined. So, either initialize it to an empty value before the loop, or just test for empty():
<i>
</i>if(empty($country)) {
$country = 'unknown';
}
Copy linkTweet thisAlerts:
@site-developerauthorApr 06.2019 — @NogDog#1602552

No matter, what IP I try, I always get the "Country Unknown" echo. Even though the country exists in the list file.

<i>
</i>&lt;?php
$two_letter_country_code=iptocountry("159.224.83.100");

function iptocountry($ip) {

<i> </i>$numbers = preg_split( "/./", $ip);

<i> </i>include("ip_files/".$numbers[0].".php");
<i> </i>$code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);

<i> </i>foreach($ranges as $key =&gt; $value){
<i> </i> if($key&lt;=$code){
<i> </i> if($ranges[$key][0]&gt;=$code){$country=$ranges[$key][1];break;}
<i> </i> }
<i> </i>}
<i> </i>if ($country==""){$country="unkown";}
<i> </i>return $country;
}

if(empty($country)) {
$country = 'unknown';
}

echo "$country";
?&gt;
×

Success!

Help @site-developer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...