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.
<?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 . "<br />";
}
if (!empty($business)) {
echo $business . "<br />";
}
if (!empty($address)) {
echo $address . "<br />";
}
if (!empty($phone)) {
echo $phone . "<br />";
}
if (!empty($fax)) {
echo $fax . "<br />";
}
if (!empty($mobile)) {
echo $mobile . "<br />";
}
if (!empty($email)) {
echo "<a href=\"mailto:$email\">$email</a>" . "<br />";
}
if (!empty($personalemail)) {
echo "<a href=\"mailto:$personalemail\">$personalemail</a>" . "<br />";
}
if(!empty($website)){
$final_url = (!preg_match("~^(?:f|ht)tps?://~i", $website))? 'http://'.$website: $website;
echo "<a href=\"$final_url\">$final_url</a>" . "<br />";
}
if(!empty($facebook)){
$final_url = (!preg_match("~^(?:f|ht)tps?://~i", $facebook))? 'http://'.$facebook: $facebook;
echo "<a href=\"$final_url\"><img src=\"/wp-content/uploads/2013/05/fb1.png\"></a>";
}
if(!empty($twitter)){
$final_url = (!preg_match("~^(?:f|ht)tps?://~i", $twitter))? 'http://'.$twitter: $twitter;
echo "<a href=\"$final_url\"><img src=\"/wp-content/uploads/2013/05/tw1.png\"></a>";
}
if(!empty($linkedin)){
$final_url = (!preg_match("~^(?:f|ht)tps?://~i", $linkedin))? 'http://'.$linkedin: $linkedin;
echo "<a href=\"$final_url\"><img src=\"/wp-content/uploads/2013/05/linkedin.png\"></a>";
}
?>