【发布时间】:2023-04-10 01:47:01
【问题描述】:
我在 Wordpress 中有一个生成表单的 php 文件,它调用自定义函数来获取用户数据以填充表单。如果未找到用户,则该函数正常运行(应返回“无效用户名”),但如果用户有效,则不会逐步执行该函数。这是它的要点:
function print_report($username, $var2, $var3)
{
$user = get_user_by('login', $username);
if($user) {
//get all the information and create the form
}
else echo "invalid username";
return false;
}
似乎当找到有效用户时,它不会单步执行该功能,而是直接跳到最后。换句话说,如果用户有效,则返回“false”。
我希望我已经很好地解释了这一点。如果您需要其他信息,请告诉我。
编辑
我按照建议做了var_dump()。它找到了用户,但仍然返回 false。这是我的测试用户的结果:
string 'jimv3000' (length=8)
object(WP_User)[1610]
public 'data' =>
object(stdClass)[1634]
public 'ID' => string '1101' (length=4)
public 'user_login' => string 'jimv3000' (length=8)
public 'user_pass' => string '*THERE IS A PASSWORD HERE*' (length=34)
public 'user_nicename' => string 'jimv3000' (length=8)
public 'user_email' => string '*THERE IS AN EMAIL HERE*' (length=18)
public 'user_url' => string '' (length=0)
public 'user_registered' => string '2016-05-16 20:37:30' (length=19)
public 'user_activation_key' => string '' (length=0)
public 'user_status' => string '0' (length=1)
public 'display_name' => string 'jimv3000' (length=8)
public 'ID' => int 1101
public 'caps' =>
array (size=1)
'subscriber' => boolean true
public 'cap_key' => string 'wp_capabilities' (length=15)
public 'roles' =>
array (size=1)
0 => string 'subscriber' (length=10)
public 'allcaps' =>
array (size=3)
'read' => boolean true
'level_0' => boolean true
'subscriber' => boolean true
public 'filter' => null
false
一如既往,感谢您的帮助。
【问题讨论】:
-
$username和$user中有什么?你能做一个var_dump($username, $user)并向我们展示你的结果吗? -
除非你告诉代码在 if 语句中“返回”某些东西,否则你总是会返回 false,因为 return 语句不在你的 if 语句中。
-
它确实找到了用户和数据。返回太长,这里发帖,但是找到了用户数据,最后还是返回“false”。
-
更新:在查看了 var_dump 之后,我有了一个见解。谢谢,@Felippe Duarte