1

I wonder if you can help me slim down my code.

I've built a bespoke share include for a client site. It works fine but I'm sure one of you clever lot can help me to make it less bulky.

I have three share URLs: $share_url_facebook, $share_url_twitter and $share_url_linkedin.

Each URL is different in its structure and is fairly complex.

I then have the following code:

echo '<a href="' . $share_url_facebook . '" title="Share this on facebook">';
echo '<i class="fa fa-facebook"></i>';
echo '</a>';
echo '<a href="' . $share_url_twitter . '" title="Share this on twitter">';
echo '<i class="fa fa-twitter"></i>';
echo '</a>';
echo '<a href="' . $share_url_linkedin . '" title="Share this on LinkedIn">';
echo '<i class="fa fa-linkedin"></i>';
echo '</a>';

Is there a way for me to create some sort of loop to prevent the repetition?


Based on the accepted solution by @JasonK here is my final(simplified) tested code:

$share = [
    'facebook' => [
        'title' => 'Facebook',
        'url'   => 'https://www.facebook.com/'
    ],
    'twitter' => [
        'title' => 'Twitter',
        'url'   => 'https://twitter.com'
    ],
    'linkedin' => [
        'title' => 'LinkedIn',
        'url'   => 'https://linkedin.com'
    ]
];
foreach ($share as $key => $details) {
    echo '<a href="' . $details['url'] . '" title="Share this on ' . $details['title'] . '">';
    echo '<i class="fa fa-' . $key . '"></i>';
    echo '</a>';
}
4
  • Different value used so its not repetition Commented Mar 20, 2018 at 10:50
  • You would have to create an array containing the 3 variable values $share_url_??? and title and the css class names fa-???? so you might even end up with more code in an attempt to create this array and then loop over it Commented Mar 20, 2018 at 10:52
  • As RiggsFolly said, create an array to store urls. Iterate over the array Commented Mar 20, 2018 at 10:55
  • Create indexed array with $shareBtns = ['socialMediaName' => 'url', ...] and iterate over that. Commented Mar 20, 2018 at 10:56

3 Answers 3

3

As you already suggested; create an array and iterate over it.

$social = [
    'facebook' => [
        'title' => 'Facebook'
        'url'   => 'https://facebook.com'
    ],
    'twitter' => [
        'title' => 'Twitter'
        'url'   => 'https://twitter.com'
    ],
    'linkedin' => [
        'title' => 'LinkedIn'
        'url'   => 'https://linkedin.com'
    ]
];

foreach ($social as $key => $details) {
    echo 'Key ' . $key;
    echo 'Title ' . $details['title'];
    echo 'URL ' . $details['url'];
}
Sign up to request clarification or add additional context in comments.

Comments

1

There is an even shorter solution to this, which uses PHPs dynamic variables and does not force you to create an array with all details.

This way you can remain your old variables. This is especially useful, if they come from a file you didnt write yourself and therefore cant/dont want to change

$social = [ 'facebook', 'twitter', 'linkedin' ];
foreach ($social as $site) {
    print '<a href="' . ${'share_url_' . $site} . '" title="Share this on ' . $site . '">';
    print '<i class="fa fa-' . $site . '"></i>';
    print '</a>';
}

3 Comments

This is incredibly useful, thank you. I find this really interesting too: ${'share_url_' . $site}
You can do basically everything inside the curly brackets. Call functions or methods for example. Keep in mind tho, that this can be hard to debug, so use it carefully.
@morgyface In virtually all cases, "variable variables" are bad advice/practice. Most programmers dabble in using them for a short while, but only until they realize they are just poor excuses for arrays. Let me save you a world of strife -- don't use variable variables.
1

You can use array to store "config". Key is a name of service, value is url or variable with url.

After that, you can iterate on this array using foreach. In foreach you can use echo or other function to output string, or asigne string to variable. I used sprintf to format all string without contacenation and to assigned it to variable http://php.net/manual/en/function.sprintf.php

$services [
  'facebook' => $share_url_facebook,
  'twitter' => $share_url_twitter
]
$output = '';
foreach ($services as $serviceName => $url) {
    $output += sprintf(
       '<a href="%s" title="Share this on %s"><i class="fa fa-%s"></i></a>', 
       $url, 
       ucfirst($serviceName),
       $serviceName
   );
}

3 Comments

Code-only answers are low value on StackOverflow because they do very little to educate the OP and thousands of future researchers. Please improve this answer with the intent to educate.
@mickmackusa I added description but I think the example is quite easy to understand and it self describing idea of this solution.
Even simple snippets deserve explanation. Never assume that future readers have complete literacy of a given language. That += to perform concatenation looks troublesome in php. It is a good idea to test your codes before posting. I normally add a demo link with my answers to double check myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.