sprintf() 与字符串连接

发布于 2024-11-01 17:17:53 字数 607 浏览 0 评论 0 原文

我今晚早些时候发布了一个问题 - PHP Wordpress 引用问题 其中一些引用导致了我一些问题。

发布了一个建议使用 echo sprintf 的答案。这看起来非常干净,并且处理了任何变量和变量。引用可能出现的问题。我的问题是,使用 sprintf 有什么缺点?如果有的话?

如果 echo 通常会导致混合 HTML 和 PHP 出现问题,为什么我们要使用它呢?作为参考,这是回显的语句:

echo "";

和 echo &冲刺:

echo sprintf(
    '<img src="%s/img/%s.png" />', 
    get_bloginfo('template_url'), 
    $f['mainImage']
);

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']
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ゞ花落谁相伴 2024-11-08 17:17:53

“引起”问题的不是 echo 语句,而是缺乏新人可用的信息,所以我将尝试解释它们。

php中有四种指定字符串的方法:

  • 使用单引号

    $str = '你好世界。这可能是一个 php $variable';

    echo $str; // 输出:你好世界。这可能是一个 php $variable

由于字符串用单引号括起来,php 引擎不会尝试将 $variable 解释为实际变量,并且您在引号中看到的内容将得到回应。

  • 使用双引号
$variable = 'random text';
$str = "Hello World. This will be interpreted as $variable";

echo $str; // 输出:你好世界。这将被解释为随机文本

在此示例中,php 将尝试查找名为 $variable 的变量并在字符串中使用其内容。

  • Heredoc 语法

Heredoc 对于诸如您想做的事情很有用 - 您有变量、单引号、双引号的混合以及转义所有可能混乱的内容。因此,优秀的 php 人员为我们实现了以下语法:

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

将会发生的情况是 PHP 引擎将尝试解释变量(和函数,但我不会发布如何执行此操作的示例,因为它可以在 php.net 上找到)你用 << 包裹了字符串

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

它与使用单引号字符串相同 - 不会发生变量替换,并且要将某些内容指定为 nowdoc - 只需用单引号字符包裹分隔符,如示例所示。

如果您能够理解这四个原则,那么字符串中的引号问题就应该消失:)

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

咆哮 2024-11-08 17:17:53

主要缺点是速度。但在大多数情况下,这并不重要。仅当您在一个大循环中打印大量信息时才会出现这种情况。
sprintf 是一个很棒的函数,您应该使用它。

但使用 printf 代替 echo sprintf 会更好。

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.

南城旧梦 2024-11-08 17:17:53

建议使用 sprintf 可能是因为它为您提供了格式化功能。我不知道 sprintf 有任何性能劣势;它可能在幕后进行相同的 C 调用。我同意 Wiseguy 的评估,尽管 printf 是做同样事情的更直接的方法。

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.

雨夜星沙 2024-11-08 17:17:53

第一个 (sprintf) 更灵活,也是一个真正的函数。后者是一种语言构造,灵活性较差,但编写起来更容易和快速。

'echo'、'sprintf' 和 'print' 不是函数,但语言构造

'sprintf' 接受输入的方式不同:您可以提供格式字符串,然后列出所有所需的输入。

对于常见情况,乍一看差异并不明显,而且通常甚至不相关。在某些特殊情况下,'sprintf' 可能会更好。

echo 的处理速度比 sprintf 更快。

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.

稚然 2024-11-08 17:17:53

如果仔细查看您引用的答案,该修复与 sprintf 无关,它使用的是 get_bloginfo() 而不是 bloginfo() >;前者返回一个字符串而不是回显它。

sprintf 更像是一种风格——有些人喜欢用 c 风格的 %s 格式字符串替换变量,其他人喜欢在字符串文字中使用 PHP 的变量扩展。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文