Using an echo after a return in an if statement

I’ve got some code, here, that’s giving me some trouble.

    function rcMetaDisplayNone($content)
    {
        global $rc_options;
        global $post;
        global $login_button;

    $rcUserLevel = get_post_meta($post->ID, 'rcUserLevel', true);

    if (!current_user_can('read') && ($rcUserLevel == 'Administrator' || $rcUserLevel == 'Editor' || $rcUserLevel == 'Author' || $rcUserLevel == 'Contributor' || $rcUserLevel == 'Subscriber'))
    {
        $userLevelMessage = strtolower($rcUserLevel);
        return do_shortcode( $rc_options[$userLevelMessage . '_message'] );
    }
    else
    {
        // return the content unfilitered
        return $content;
    }
}

The task I’m trying to conquer, here, is how to append an echo to the end of the if statement, like so:

Read More
return do_shortcode( $rc_options[$userLevelMessage . '_message'] );
echo $login_link;

Now, by rules of how PHP works, I understand that this shouldn’t work. My question is: how would I make it work? Is there a function or a workaround that will let me do this?

I’m a bit of a beginner to PHP, so I apologize if the solution is obvious.

**UPDATE**

Holy answers, Batman! Thanks for the help, folks. I should clarify. I’ve updated the code to include the entire function.

The $login_link code is essentially outputting a link that allows the user to log in (ironically enough). The return code, however, is outputting a message that, in context to this WordPress plugin I am using, is stating that the user does not have permission to access the content on the page.

See the dilemma here? I am trying to append a log-in link to this message. It’s not simply an empty return value, but an actual message outputting to the browser.

**UPDATE 2**

This is a shortcode I’m using, so I’m not sure I can output said code outside of the function.

Related posts

Leave a Reply

5 comments

  1. You cannot execute code after a return statement (in the function returning).

    If I understand your problem correctly (executing do_shortcode() then showing the value of $login_link, then returning), then: Store the return value, call echo, and finally return the stored value.

    $returnValue = do_shortcode( $rc_options[$userLevelMessage . '_message'] );
    echo $login_link;
    return $returnValue;
    

    Edit: Based on your update, I believe you’re looking for the following:

    $returnValue = do_shortcode( $rc_options[$userLevelMessage . '_message'] );
    return $returnValue . $login_link;
    
  2. you would echo outside of the function. Rule of thumb you should never echo INSIDE the function always return the output and echo the return set

    So you’d do

    <?php 
    $echovar = rcMetaDisplayNone($content);
    
    echo $echovar;
    
    ?>
    

    UPDATE USING YOUR FULL CODE

    <?php
    
    function rcMetaDisplayNone($content)
        {
            global $rc_options;
            global $post;
            global $login_button;
    
        $rcUserLevel = get_post_meta($post->ID, 'rcUserLevel', true);
        $staticstring = "<a href="blah.php">THIS IS MY STATIC STRING IT CAN CONTAIN ANYTHING</a>";
    
        if (!current_user_can('read') && ($rcUserLevel == 'Administrator' || $rcUserLevel == 'Editor' || $rcUserLevel == 'Author' || $rcUserLevel == 'Contributor' || $rcUserLevel == 'Subscriber'))
        {
            $userLevelMessage = strtolower($rcUserLevel);
            $output = do_shortcode( $rc_options[$userLevelMessage . '_message'] );
        }
        else
        {
            // return the content unfilitered
            $output = $content;
        }
        return $output.$staticstring;
    }
    
    echo rcMetaDisplayNone($content);
    ?>
    
  3. This is not just PHP but the rules to how functions work. Once a return is hit, the function is done. This is so even if there is more lines after the return.

    Function test(){
      echo "test";
      return;
      echo "done";
    }
    

    This will only output "test" since the return is hit. If you want to do anything, you need to make it happen before the end of a function, not when it’s done.

    echo $login_link;
    return do_shortcode( $rc_options[$userLevelMessage . '_message'] );
    

    So you can echo out the variable before returning.

    EDIT:
    Since what you are looking for is to add to the message after do_shortcode, you can concatenate the string before returning. Like so:

    $shortcode = do_shortcode( $rc_options[$userLevelMessage . '_message'] );
    return $shortcode."possible HTML".$login_link;
    

    The “possible HTML” can be any string or formatting you may wish. Research PHP String Concatenation

  4. It doesn’t make sense, you could just move it to above the return:

    echo $login_link;
    return do_shortcode( $rc_options[$userLevelMessage . '_message'] );
    

    It might be made more sense of if you gave us more of an idea of what you actually want to do (in a less abstract way than “output a value”).

  5. You can’t. Return will finish the function.
    You can return what you need and then use echo where you call the function:

    echo rcMetaDisplayNone($someParam);
    

    You can return array of walues if you need more.

    $returned = rcMetaDisplayNone($someParam);
    echo $returned['echoVal'];