sprintf() 与字符串连接
我今晚早些时候发布了一个问题 - PHP Wordpress 引用问题 其中一些引用导致了我一些问题。
发布了一个建议使用 echo sprintf 的答案。这看起来非常干净,并且处理了任何变量和变量。引用可能出现的问题。我的问题是,使用 sprintf 有什么缺点?如果有的话?
如果 echo
通常会导致混合 HTML 和 PHP 出现问题,为什么我们要使用它呢?作为参考,这是回显的语句:
echo "";
和 echo &冲刺:
echo sprintf(
'<img src="%s/img/%s.png" />',
get_bloginfo('template_url'),
$f['mainImage']
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
“引起”问题的不是 echo 语句,而是缺乏新人可用的信息,所以我将尝试解释它们。
php中有四种指定字符串的方法:
使用单引号
$str = '你好世界。这可能是一个 php $variable';
echo $str; // 输出:你好世界。这可能是一个 php $variable
由于字符串用单引号括起来,php 引擎不会尝试将 $variable 解释为实际变量,并且您在引号中看到的内容将得到回应。
echo $str; // 输出:你好世界。这将被解释为随机文本
在此示例中,php 将尝试查找名为 $variable 的变量并在字符串中使用其内容。
Heredoc 对于诸如您想做的事情很有用 - 您有变量、单引号、双引号的混合以及转义所有可能混乱的内容。因此,优秀的 php 人员为我们实现了以下语法:
将会发生的情况是 PHP 引擎将尝试解释变量(和函数,但我不会发布如何执行此操作的示例,因为它可以在 php.net 上找到)你用 << 包裹了字符串
它与使用单引号字符串相同 - 不会发生变量替换,并且要将某些内容指定为 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.
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 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:
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 <<
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 :)
主要缺点是速度。但在大多数情况下,这并不重要。仅当您在一个大循环中打印大量信息时才会出现这种情况。
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.
建议使用 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.
第一个
(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 thansprintf.
如果仔细查看您引用的答案,该修复与 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 usingget_bloginfo()
instead ofbloginfo()
; 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.