0

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.

1 Answer 1

1

If the user does not exist then your template should return something to say that the user does not exist instead of attempting to echo out their data, example:

<?php $keyuser = get_user_by('login', $username); ?>
<?php if(empty($keyuser)): ?>
    <p>That username does not exist, please check for typos or try a different search cryteria.</p>
<?php else: ?>
    <table>
        <tr>
            <th>User ID</th>
            <th>Username</th>
            <th>Email Address</th>
            <th>Roles</th>
        </tr>
        <tr>
            <td><?php echo $keyuser->ID; ?></td>
            <td><?php echo $keyuser->user_login; ?></td>
            <td><?php echo $keyuser->user_email; ?></td>
            <td><?php echo $keyuser->roles[0]; ?></td>
        </tr>
    </table>
<?php endif; ?>

If this is not feasible and you need to set the variables to a blank string then you could use a ternary if statement if you think that looks neater / to reduce typing

$keyuser = get_user_by('login', $username);
$byusername_dename = $keyuser->user_login ?: '';
$byusername_deemail = $keyuser->user_email ?: '';
$byusername_hisrole = $keyuser->roles[0] ?: '';
$byusername_id = $keyuser->ID ?: '';
5
  • Thanks alot @bob the ternary operator is best for me, i hope is not an operator that will become obsolete soon? But so far it really helped and make my code shorter.
    – pandglobal
    Commented May 15, 2020 at 17:38
  • 1
    @pandglobal I doubt it will become obsolete any time soon, it was introduced in PHP 5.3 but I don't see any notes about it being deprecated (which would happen sometime before it gets removed) and they introduced ?? in PHP 7 which is slightly different but related so would be odd to take it out after that. php.net/manual/en/…
    – Bob
    Commented May 15, 2020 at 18:04
  • Thanks for now i will use it, but will keep watch of it, because if it becomes obsolete it will really break many things in my work, thanks alot @Bob for pointing it to me
    – pandglobal
    Commented May 15, 2020 at 18:15
  • pandglobal, if Bob's answer addressed provided a correct/working solution please flag it as accepted by clicking the little checkmark under the vote count on your original question. Commented May 15, 2020 at 19:36
  • I have done that @tony, thanks
    – pandglobal
    Commented May 17, 2020 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.