Jump to content

Too many IFs?


halben
Go to solution Solved by vinny42,

Recommended Posts

Can someone tell me if this is excessive use of IF statement?

 

Thanks.

if ($role == 'admin') {

    // Check if the user exist
    if (isset($_POST['user_email']) && !empty($_POST['user_email'])) {

        // Sanitize the data
        $user_email_data = trim(strip_tags(stripslashes($_POST['user_email'])));

        // Now use PHP to check for validation
        if (filter_var($user_email_data, FILTER_VALIDATE_EMAIL)) {
            if (false == get_user_by('email', $user_email_data)) {
                // the user doesn't exist
            } else {
                // the user exists
                update_user_meta($userID, 'wp_user_roles', '10');
                // check wp if the new value has been stored
                if (get_user_meta($userID, 'wp_user_roles', true) != '10') {
                    wp_die('An error occured!');
                }
            }
        }
    }
} else {
    // Do nothing, exit
    exit ;
}
Link to comment
Share on other sites

IMO no, but you could very easily clean some of them up:

if ($role != 'admin') {
    // Do nothing, exit
    exit ;
}

// Check if the user exist
if (isset($_POST['user_email']) && !empty($_POST['user_email'])) {

    // Sanitize the data
    $user_email_data = trim(strip_tags(stripslashes($_POST['user_email'])));

    // Now use PHP to check for validation
    if (filter_var($user_email_data, FILTER_VALIDATE_EMAIL) && get_user_by('email', $user_email_data)) {
        // the user exists
        update_user_meta($userID, 'wp_user_roles', '10');
        // check wp if the new value has been stored
        if (get_user_meta($userID, 'wp_user_roles', true) != '10') {
            wp_die('An error occured!');
        }
    }
}
Link to comment
Share on other sites

  • Solution

the fact that you think it might be too much means it is too much.

PHP can handle it, no problem, but the code is hard to read. it would be better to split the IF's up. and maybe split the code into re-usable functions.

 

Nested IF statements generally become easier to read if you test the 'fail' state first, because the amount of code to handle the fail state is usually much shorter than the success code, which means that the ELSE will be close to the IF and that helps to keep track of the nesting.

 

 

By the way, your code itself has a few holes :-)

 

$user_email_data = trim(strip_tags(stripslashes($_POST['user_email'])));

 

There are no slashes in the $_POST unless you are using the *extremely* evil magic_quotes, in which case you should probably get rid of them asap.

There are also no HTML tags in the address. If there are then it's not an emailaddress so removing the tags will not magically create an email adress, you might aswell stop there.

 

I'm not familiar with wp but I'm sure update_user_meta() will return some value to indicate success and failure. running an extra query to see if it works sounds unnecessary (and just returning "something went wrong" doesn't tell the user much (or you).

Link to comment
Share on other sites

Not my favorite, but fewer ifs for illustration.  Short-circuit and fail on the first false: 

if ($role == 'admin' &&
    isset($_POST['user_email']) &&
    filter_var($_POST['user_email'], FILTER_VALIDATE_EMAIL) &&
    get_user_by('email', $_POST['user_email'])
{
        update_user_meta($userID, 'wp_user_roles', '10');

        if (get_user_meta($userID, 'wp_user_roles', true) != '10') {
            wp_die('An error occured!');
        }
}
Edited by AbraCadaver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.