Using get_bloginfo(‘template_directory’) or variable – performance issue

Can I please ask you about the performance of these two approaches in term of execution speed and server load?

approach 1:

Read More
<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?

Related posts

1 comment

  1. 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 from get_option() calls, and these are cached internally with wp_cache_set() and fetched with wp_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:

    $template_dir = get_template_directory_uri(); 
    
    foreach ( array ( 1, 2, 3 ) as $n )
        echo "<img src='$template_dir/data1/images/$n.jpg' alt=''>";
    

Comments are closed.