Reputation: 602
In many WordPress Themes and Frameworks I found these lines of code:
<!--[if lt IE 7]><html <?php language_attributes(); ?> class="no-js lt-ie9 lt-ie8 lt-ie7"><![endif]-->
<!--[if IE 7]><html <?php language_attributes(); ?> class="no-js lt-ie9 lt-ie8"><![endif]-->
<!--[if IE 8]><html <?php language_attributes(); ?> class="no-js lt-ie9"><![endif]-->
<!--[if gt IE 8]><!--> <html <?php language_attributes(); ?> class="no-js"><!--<![endif]-->
I noticed the multiple calls for language_attributes()
which I think is unnecessary time spent even for a simple function like this one, so I used this code:
<?php ob_start(); language_attributes(); $language_attributes = ob_get_clean(); ?>
<!--[if lt IE 7]><html <?php echo $language_attributes; ?> class="no-js lt-ie9 lt-ie8 lt-ie7"><![endif]-->
<!--[if IE 7]><html <?php echo $language_attributes; ?> class="no-js lt-ie9 lt-ie8"><![endif]-->
<!--[if IE 8]><html <?php echo $language_attributes; ?> class="no-js lt-ie9"><![endif]-->
<!--[if gt IE 8]><!--> <html <?php echo $language_attributes; ?> class="no-js"><!--<![endif]-->
<?php unset($language_attributes); ?>
to avoid calling language_attributes()
multiple times, but the more I see the use of the first pattern the more I doubt my decision so I decided to ask for an expert opinion.
So, here's my questions:
in a broader aspect - not limited to this specific function - if the function do the exact every time
What's the benefit of calling a function multiple times over storing the returned value?
Like, is there some hidden caching mechanism in php
that do run-time caching of a function call so this way i don't need to store the returned value.
Upvotes: 0
Views: 571
Reputation: 78994
Without following the convoluted Wordpress code chain (I hate Wordpress) there is no way to know. The actual invocation of the function obviously adds time, though it is probably negligible, but this function call executes at least two other function calls in this case.
If any of the functions are doing anything substantial (database query, including files, looping, extensive regex, etc.) then this could compound into performance problems.
In general best practice, especially if you don't know what all of the functions are doing, it would be best to call it once. Unless of course there is the possibility of the return / result changing across calls and you need the differing information.
You can do it using:
$language_attributes = get_bloginfo('language');
Or how that works:
$language_attributes = str_replace('_', '-', get_locale());
Function Reference/get bloginfo
Upvotes: 1
Reputation: 174
To answer your question as directly as I can, it depends. Does the function do the exact same thing each time, with a performance cost? Is the function cached / returning an easily calculable value without touching storage? Will it be opcached or otherwise runtime-cached? All of these affect your decisions about the call.
Beyond that there are even more questions to ask. Why are you concerned with this call? Have you measured its impact? Have you measured higher grains? Other parts of the stack? There are likely tons of optimizations that would be more effective, assuming you have not measured anything.
With all that said, there is a good chance this is premature optimization, not something you should consider yourself with. I recommend The Mature Optimization Handbook, which covers this topic extensively, and should help you on your path.
Upvotes: 1