Home > database >  PHP concat operator will move the content to the beginning
PHP concat operator will move the content to the beginning

Time:09-30

I write a PHP code snippet in WordPress, as below:

            <td><?php echo '<!-- 2021-09-29: Replace Font Awesome tag <i class="fas fa-cloud-download-alt"></i> with the SVG sprite, to improve performance. -->
            <svg class="my-svg-inline--fa my-fa-w-20">
                <use href="' . bloginfo('template_directory') . '/images/icons/icons.svg#cloud-download-alt"/>
            </svg><a download-link="'.site_url( '/' ).$post->post_name.'-download-thanks" href="'.$link_url.'" class="download-url"> Download</a>' ?></td>

What I expect is that bloginfo('template_directory') will be inserted into the href, as below:

<td><svg class="my-svg-inline--fa my-fa-w-20">
                <use href="https://www.example.com/wp-content/themes/mytheme/images/icons/icons.svg#cloud-download-alt"/>
            </svg><a download-link="xxx" href="xxx" class="download-url"> Download</a></td>

But the actual output is:

<td>https://www.example.com/wp-content/themes/mytheme
            <svg class="my-svg-inline--fa my-fa-w-20">
                <use href="/images/icons/icons.svg#cloud-download-alt"/>
            </svg><a download-link="xxx" href="xxx" class="download-url"> Download</a></td>

The path generated by bloginfo('template_directory') is put before the string generated by PHP echo, why?

Update

Sorry for missing the information about the function bloginfo. I will try get_bloginfo and update this post.

CodePudding user response:

It sounds like bloginfo() isn't returning anything, it's doing its own echoing. This happens before the function returns, so it gets printed before the caller concatenates the result and prints its resulting string.

Either change bloginfo() to return the string instead of printing it, or don't use concatenation. Print the first part, then call bloginfo(), then print the second part.

<td><?php echo '<!-- 2021-09-29: Replace Font Awesome tag <i class="fas fa-cloud-download-alt"></i> with the SVG sprite, to improve performance. -->
<svg class="my-svg-inline--fa my-fa-w-20">
    <use href="';
bloginfo('template_directory');
echo '/images/icons/icons.svg#cloud-download-alt"/>
</svg><a download-link="'.site_url( '/' ).$post->post_name.'-download-thanks" href="'.$link_url.'" class="download-url"> Download</a>' ?></td>
  •  Tags:  
  • php
  • Related