Can I please ask you about the performance of these two approaches in term of execution speed and server load?
approach 1:
<img src="<?php echo get_bloginfo('template_directory'); ?>/data1/images/1.jpg">
<img src="<?php echo get_bloginfo('template_directory'); ?>/data1/images/2.jpg" />
<img src="<?php echo get_bloginfo('template_directory'); ?>/data1/images/3.jpg" />
approach 2:
<?php $variable= get_bloginfo('template_directory'); ?>
<img src="<?php echo $variable; ?>/data1/images/1.jpg">
<img src="<?php echo $variable; ?>/data1/images/2.jpg" />
<img src="<?php echo $variable; ?>/data1/images/3.jpg" />
The answer to this question will be very useful for me since I meet such cases many times during WordPress developement. Is the time to get variable content less than the time querying the database for blog info?
There is no performance difference, because the result of
get_bloginfo()
comes from an internal cache anyway, because most (all?) of the return values come fromget_option()
calls, and these are cached internally withwp_cache_set()
and fetched withwp_cache_get()
. See Exploring the WordPress Cache API.Even if there would be a difference it would be too small to be relevant.
The more important difference is readability. This is easier to read and less error prone: