Php 需要帮助从功能文件中提取国家/地区cc

Php 需要帮助从功能文件中提取国家/地区cc,php,function,Php,Function,我有以下代码通过IP检查国家/地区数据库。当我使用以下命令时,它会起作用 $userInfo = geoip_detect2_get_info_from_current_ip(); if ($userInfo->country->isoCode == 'US') echo 'Hallo! Schön dass Sie hier sind!'; 现在,我想把它放在我的函数文件中,只返回作为lowerstring的国家代码,然后我可以在整个站点中引用它 因此,在这种情况下,它将返回“我

我有以下代码通过IP检查国家/地区数据库。当我使用以下命令时,它会起作用

$userInfo = geoip_detect2_get_info_from_current_ip();
if ($userInfo->country->isoCode == 'US')
echo 'Hallo! Schön dass Sie hier sind!';
现在,我想把它放在我的函数文件中,只返回作为lowerstring的国家代码,然后我可以在整个站点中引用它

因此,在这种情况下,它将返回“我们”,而不是“我们”

抱歉如果这是个简单的问题,我是新手

谢谢



像这样的方法应该会奏效:

function getCountryCode() 
{
    $userInfo = geoip_detect2_get_info_from_current_ip();
    return strtolower($userInfo->country->isoCode);
}
进一步,您可以检查
$userInfo->country->isoCode
是否有任何值

function getCountryCode() 
{
    $userInfo = geoip_detect2_get_info_from_current_ip();
    if (!empty($userInfo->country->isoCode)) {
        return strtolower($userInfo->country->isoCode);
    } else {
        return '';   // or some default country code
    }
}