开发者

PHP sprintf vs. echo

开发者 https://www.devze.com 2023-02-24 06:24 出处:网络
I posted a question earlier tonight - PHP Wordpress quotes issue where some quotes were causing me some issues.

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开发者_运维问答']
);


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 :)


The main disadvantage is speed. But in most cases, it doesn't matter. It would only if you were printing a lot of information, in a big loop. sprintf is a great function, and you should use it.

But using printf instead of echo sprintf would be better.


The sprintf was probably suggested for the formatting capabilities it gives you. I'm not aware of any performance disadvantage to sprintf; it's probably making an identical C call under the hood. I agree with Wiseguy's assessment though that printf is a more straightforward way to do the same thing.


First (sprintf) is more flexible and a real function. Latter one is a language construct, less flexible but more easy and quick to write.

'echo','sprintf' and 'print' are not a functions, but a language constructs

'sprintf' takes input differently: you can provide a format string and then list all the required input.

For common cases the difference is not obvious at first sight, and quite often not even relevant. In some special cases 'sprintf' can be better.

But echo processes faster than sprintf.


If you look closely at the answer you reference, the fix had nothing to do with sprintf, it was using get_bloginfo() instead of bloginfo(); the former returns a string rather than echoing it.

The sprintf is more of a style thing -- some people like to substitute their variables with c-style %s format strings, others like to use PHP's variable expansion in string literals.

0

精彩评论

暂无评论...
验证码 换一张
取 消