在我的错误日志中,我收到了很多 PHP 消息:PHP Notice: Undefined variable: genderName in for all the variables under $UserData['']. 它们是未定义的,因为我试图使用这个数组来定义它们。我想知道定义变量的最佳方法是什么,这样我就不会再在日志中遇到问题了。
if (!$location) $location = "-";
$UserData = array();
$UserData['id'] = $UserInfo['user_id'];
$UserData['username'] = $UserInfo['username'];
$UserData['gender'] = $genderName;
$UserData['age'] = $age;
$UserData['photo'] = $photo;
$UserData['location'] = $location;
$UserData['description'] = $description;
$UserData['isAdminORModerator'] = $typeUser;
$UserData['profile'] = SITE."/".$UserInfo['username'];
$UserData['level'] = $level;
}
return $UserData;
这是所有问过的人的完整代码。
function commonData($uid)
{
if ($uid)
{
$sql = "
SELECT a.user_id, a.email, a.username, a.displayname, a.level_id, a.photo_id
FROM engine4_users AS a
WHERE a.user_id = ".$uid;
}
$UserInfo = @mysql_fetch_assoc(mysql_query($sql));
if ($UserInfo['user_id'])
{
if ($UserInfo['photo_id'] && $UserInfo['photo_id']!="NULL")
{
$PPhoto = @mysql_fetch_assoc(mysql_query("SELECT a.* FROM engine4_storage_files AS a WHERE a.file_id = ".$UserInfo['photo_id']));
$photo = SOFTLAYER.$PPhoto['storage_path'];
}
else $photo = NO_PHOTO;
$queryMoreProfile = mysql_query("SELECT * FROM engine4_user_fields_values AS a WHERE a.item_id = ".$UserInfo['user_id']);
while ($moreProfile = @mysql_fetch_assoc($queryMoreProfile))
{
//birthday
if ($moreProfile['field_id']==6)
{
$age = getAge($moreProfile['value']);
}
//about
if ($moreProfile['field_id']==13)
{
$description = $moreProfile['value'];
}
//position
if ($moreProfile['field_id']==17)
{
$gender = $moreProfile['value'];
$gendersql = @mysql_fetch_assoc(mysql_query("SELECT a.* FROM engine4_user_fields_options AS a WHERE a.option_id = ".$gender));
$genderName = $gendersql['label'];
}
//location
if ($moreProfile['field_id']==24)
{
$locationNumber = $moreProfile['value'];
$locationsql = @mysql_fetch_assoc(mysql_query("SELECT a.* FROM engine4_user_fields_options AS a WHERE a.option_id = ".$locationNumber));
$location = $locationsql['label'];
}
//level
if ($UserInfo['level_id']==1 or $UserInfo['level_id']==2)
{
$typeUser = '<isAdmin>true</isAdmin>';
$level = 'admin';
}
else if ($UserInfo['level_id']==3)
{
$level = 'moderator';
$typeUser = '<isModerator>true</isModerator>';
}
else if ($UserInfo['level_id']==9 or $UserInfo['level_id']==10 or $UserInfo['level_id']==11 or $UserInfo['level_id']==12 or $UserInfo['level_id']==13 or $UserInfo['level_id']==14)
{
$level = 'premium';
$typeUser = '';
}
else if ($UserInfo['level_id']==8)
{
$level = 'VIP';
$typeUser = '';
}
else
{
$typeUser = '';
$level = 'guest';
}
if (!$location) $location = "-";
$UserData = array();
$UserData['id'] = $UserInfo['user_id'];
$UserData['username'] = $UserInfo['username'];
$UserData['gender'] = $genderName;
$UserData['age'] = $age;
$UserData['photo'] = $photo;
$UserData['location'] = $location;
$UserData['description'] = $description;
$UserData['isAdminORModerator'] = $typeUser;
$UserData['profile'] = SITE."/".$UserInfo['username'];
$UserData['level'] = $level;
}
return $UserData;
} }