Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4325086
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:13:12+00:00 2026-05-21T09:13:12+00:00

I posted a question earlier tonight – PHP WordPress quotes issue where some quotes

  • 0

I posted a question earlier tonight – PHP WordPress quotes issue where some quotes were causing me some issues.

An answer was posted suggesting using echo sprintf. This looked very clean and took care of any variable & quoting issues that may occur. My question is, whats the disadvantage of using sprintf? If any?

Why do we use echo if it usually causes issues with mixing HTML and PHP. For reference this was the echoed statement:

echo "<img src='"; bloginfo('template_url'); echo "img/" . $f['mainImage'] . ".png' />";

and the echo & sprintf:

echo sprintf(
    '<img src="%s/img/%s.png" />', 
    get_bloginfo('template_url'), 
    $f['mainImage']
);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:13:12+00:00Added an answer on May 21, 2026 at 9:13 am

    It’s not the echo statement that “causes” problems, it’s the lack of information available to newcomers, so I’ll try to explain them.

    There are four ways of specifying a string in php:

    • Using a single quote

      $str = 'Hello World. This might be a php $variable';

      echo $str; // Outputs: Hello World. This might be a php $variable

    Since the string was wrapped in single quote, php engine will not try to interpret $variable as actual variable and the contents of what you see in the quotes will be echoed.

    • Using double quote
    $variable = 'random text';
    $str = "Hello World. This will be interpreted as $variable";
    

    echo $str; // Outputs: Hello World. This will be interpreted as random text

    In this example, php will try to find a variable named $variable and use its contents in the string.

    • Heredoc syntax

    Heredoc is useful for things such as what you wanted to do – you have a mix of variables, single quotes, double quotes and escaping all that can be a mess. Hence, good php people implemented the following syntax for us:

    $str = <<<EOF
    <img src="$directory/images/some_image.gif" alt='this is alt text' />
    <p>Hello!</p>
    EOF;
    

    What will happen is that PHP engine will try to interpret variables (and functions, but I won’t post examples on how to do that since it’s available at php.net), however you wrapped the string with <<

    • Nowdoc syntax
    $str = <<<'EOF'
        <p>Hello! This wants to be a $variable but it won't be interpreted as one!</p>
        EOF;
    

    It’s the same as using a single-quoted string – no variable replacements occur, and to specify something as nowdoc – simply wrap the delimiter with single quote characters as shown in the example.

    If you are able to understand these four principles, problems with quotes in your string should go away 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ask because my buddy posted a question earlier on How to resolve issue
Hi I posted a question a little earlier and I had some replies that
I posted this question: https://stackoverflow.com/questions/418597/java-and-net-for-php-programmer and the answers I was given didn't really help
I posted a question earlier today when I'd not zeroed in quite so far
I posted a question earlier about stopping all sounds in a swf. Now, to
I had posted one question earlier jQuery inconsistency in setting readonly attribute in IE-8
[This is an updated version of a question posted earlier, the previous title was
This is a follow-up to an earlier question I posted on EF4 entity keys
I had posted another question earlier about deployment with Passenger. That problem turned out
Earlier I posted the following question: How can I convert this date in Java?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.