Sam Skirrow Sam Skirrow - 6 months ago 9
HTML Question

php best practices for returning multiple lines of content in an IF statement

I am writing a php plugin for wordpress. I'm trying to be mindful of clean coding and want to know the best practice for returning several lines of HTML code in an IF statement.

Obviously I know about

echo
but I thought I had seen a technique like this used, but it doesn't seem to work for me. The idea being that you create several $content variables and then return it outside of the IF statement.

function signup() {
if(!Skizzar_Registration::is_skizzar_site_active()) {
$content = '<div class="signup">';
$content .= '<h1>Sign up</h1>';
$content .= '</div>';
} else {
$content = '<div class="signed_up">you are already signed up</div>';
}

return $content;

}


Currently though this returns nothing when I call the function

Answer

As you have already discovered, the specific issue is that you are not ecxhoing the data.

As per your followon question (which is prefered, return or echo), in wordpress there seems to be a convention where both options are offered with the functions named accordingly:

//echos
function the_signup_form(){
    echo get_the_signup_form();
}

//returns
function get_the_signup_form(){
    if(!Skizzar_Registration::is_skizzar_site_active()) {
        $content  = '<div class="signup">';
        $content .=     '<h1>Sign up</h1>';
        $content .= '</div>';
    } else {
        $content  = '<div class="signed_up">you are already signed up</div>';
    }

    return $content;
}