i want a more better way to change a variable or variables values if the search query returns empty, because most times if the search query returns empty and you have places you called the variables it will print undefined variables error, especially if the variables are called as array keys like this $get->user_login; it throws objects undefined error. Below is my full code:
$keyuser = get_user_by( 'login', $username );
if (!empty($keyuser)) { // only show this variables if the query returned is not empty
$byusername_dename = $keyuser->user_login; // get the username searched by username method
$byusername_deemail = $keyuser->user_email; // get the email searched by username method
$byusername_hisrole = $keyuser->roles[0]; // am searching user role based on if the user used the by username method
$byusername_id = $keyuser->ID;
}else{
$byusername_dename = ''; // get the username searched by username method
$byusername_deemail = ''; // get the email searched by username method
$byusername_hisrole = ''; // am searching user role based on if the user used the by username method
$byusername_id = '';
}
So my method of escaping those errors was to use an if statement to call the variables if not empty, but if it is empty i recall them in an else statement and then assign an empty values to them. This worked and the undefined errors are not shown if search query is empty, but i need a better way to assign empty values to the variables if search query returns empty without me having to recall them in an else statement. and if i use
}else{
return;
}
Nothing works, as the variables called anywhere else wont be valid or seems not to exit anymore. Please i need a better way and a more simpler way of doing this.