Displaying fields in a sidebar of a custom page template I'm beginning with PHP programming and would appreciate a critique on my code and how I could make it more efficient, rather than repeating the if statements.

What I'm basically doing is getting the fields typed in by the user and displaying these fields in a sidebar of a custom page template.

&lt;?php


            $name = (get_field('name'));
            $address = (get_field('address'));
            $business= (get_field('company'));
            $phone= (get_field('phone'));
            $fax= (get_field('fax'));
            $mobile= (get_field('mobile'));
            $email =  (get_field('email'));
            $personalemail =  (get_field('second_email'));
            $website =  (get_field('website'));
            $facebook =  (get_field('facebook'));
            $twitter =  (get_field('twitter'));
            $linkedin =  (get_field('linkedin'));

            if (!empty($name)) {
                echo $name . "&lt;br /&gt;";
            }

            if (!empty($business)) {
                echo $business . "&lt;br /&gt;";
            }

            if (!empty($address)) {
                echo $address . "&lt;br /&gt;";
            }

            if (!empty($phone)) {
                echo $phone . "&lt;br /&gt;";
            }

            if (!empty($fax)) {
                echo $fax . "&lt;br /&gt;";
            }

            if (!empty($mobile)) {
                echo $mobile . "&lt;br /&gt;";
            }

            if (!empty($email)) {
                echo "&lt;a href=\"mailto:$email\"&gt;$email&lt;/a&gt;" . "&lt;br /&gt;";
            }

            if (!empty($personalemail)) {
                echo "&lt;a href=\"mailto:$personalemail\"&gt;$personalemail&lt;/a&gt;" . "&lt;br /&gt;";
            }

            if(!empty($website)){
                $final_url = (!preg_match("~^(?:f|ht)tps?://~i", $website))? 'http://'.$website: $website;
                echo "&lt;a href=\"$final_url\"&gt;$final_url&lt;/a&gt;" . "&lt;br /&gt;";

            }

            if(!empty($facebook)){
                $final_url = (!preg_match("~^(?:f|ht)tps?://~i", $facebook))? 'http://'.$facebook: $facebook;
                echo "&lt;a href=\"$final_url\"&gt;&lt;img src=\"/wp-content/uploads/2013/05/fb1.png\"&gt;&lt;/a&gt;";

            }

            if(!empty($twitter)){
                $final_url = (!preg_match("~^(?:f|ht)tps?://~i", $twitter))? 'http://'.$twitter: $twitter;
                echo "&lt;a href=\"$final_url\"&gt;&lt;img src=\"/wp-content/uploads/2013/05/tw1.png\"&gt;&lt;/a&gt;";

            }

            if(!empty($linkedin)){
                $final_url = (!preg_match("~^(?:f|ht)tps?://~i", $linkedin))? 'http://'.$linkedin: $linkedin;
                echo "&lt;a href=\"$final_url\"&gt;&lt;img src=\"/wp-content/uploads/2013/05/linkedin.png\"&gt;&lt;/a&gt;";

            }

            ?&gt;
Your code can be simplified a bit.
The parantheses on the right side is not needed. Instead of

        $name = (get_field('name'));


you can just say

        $name = get_field('name');


However, since you are outputting the values directly, I'd use another approach for preparation. Collect all output strings in an array:

        $result = array();
        $result[] = get_field('name');
        $result[] = get_field('address');
        $result[] = get_field('company');
        $result[] = get_field('phone');
        $result[] = get_field('fax');
        $result[] = get_field('mobile');
        $result[] = mailTo(get_field('email'));
        $result[] = mailTo(get_field('second_email'));
        $result[] = linkTo(get_field('website'));
        $result[] = linkTo(get_field('facebook'), '&lt;img src="/wp-content/uploads/2013/05/fb1.png"&gt;');
        $result[] = linkTo(get_field('twitter'), '&lt;img src="/wp-content/uploads/2013/05/tw1.png"&gt;');
        $result[] = linkTo(get_field('linkedin'), '&lt;img src="/wp-content/uploads/2013/05/linkedin.png"&gt;');


The functions mailTo and linkTo will build the links; I'll describe them later. The point is, that now you have a single array with all lines. Some of the lines will be empty, because no data was provided. You can filter those empty lines easily with

         $result = array_filter($result);


To show all the lines with a &lt;br /&gt; in between, you just implode the array into a single string and echo it:

         echo implode('&lt;br /&gt;', $result);


Now your main functionality is done, and the only thing left is to put the formatting code, that you used repeatedly, into proper functions.

function mailTo($address)
{
    if (isValidEmailAddress($address)) {
        return printf('&lt;a href="mailto:%1$s"&gt;%1$s&lt;/a&gt;', $address);
    } else {
        return '';
    }
}


With this function, you have one single place, where the rendering of an email address is defined, so, if you want to change the appearance (eg. link it to a contact page instead of using mailto), you only have to that once.

Here a new function is introduced to check the validity of the email address. To keep it close to what your original code did, for now we'll only check if it is non-empty.

function isValidEmailAddress($address)
{
    $address = trim($address);
    return !empty($address);
}


You can of course add other validations to it.

The same has to be done to URLs.

function linkTo($url, $linkText = null)
{
    $url = trim($url);
    if (empty($url)) {
        return '';
    }

    if (is_null($linkText)) {
        $linkText = $url;
    }

    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
        $url = 'http://' . $url;
    }

    return sprintf('&lt;a href="%s"&gt;%2&lt;/a&gt;', $url, $linkText);
}


As you can see, putting repetitive code sequences into their own functions increases the readability of your code and make future changes easier.