为什么这个数字在 php if 语句中不起作用?
我定义了一个名为 $thumbs_number.
的变量。
我做了 var_dump
:
string(52) "2"
和 print_r
2
看起来 $thumbs_number
是 2现在。 但然后我这样做:
<?php if ( $thumbs_number == 2 ) : ?>
<span><?php _e( 'two likes' ); ?></span>
<?php else : ?>
<span><?php _e( 'likes' ); ?></span>
<?php endif; ?>
我仍然得到“喜欢”而不是“两个喜欢”
我做错了什么?
编辑:
这是来自以下位置的变量:
<?php $thumbs_number = wp_gdsr_render_article_thumbs($template_id = 46, $read_only = false, $stars_set = "", $stars_size = 0, $stars_set_ie6 = "", $echo = false); ?
I defined a variable called $thumbs_number.
I did var_dump
:
string(52) "2"
and print_r
2
It looks like $thumbs_number
is 2 right now.
But then I do this:
<?php if ( $thumbs_number == 2 ) : ?>
<span><?php _e( 'two likes' ); ?></span>
<?php else : ?>
<span><?php _e( 'likes' ); ?></span>
<?php endif; ?>
I'm still getting 'likes' not 'two likes'
What I'm doing wrong?
EDIT:
here is the variable comming from:
<?php $thumbs_number = wp_gdsr_render_article_thumbs($template_id = 46, $read_only = false, $stars_set = "", $stars_size = 0, $stars_set_ie6 = "", $echo = false); ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://plugins.svn .wordpress.org/gd-star- rating/tags/1.9.9/code/fn/legacy.php 在文档中说“@return string html with generated content”。它可能不只是返回数字或空格,还返回 html 标签(您可以通过在输出结果上使用“查看源”来检查这一点。如果您只是在浏览器中查看它,它会解析 html-标签,您将看不到它们。)
简单的修剪是不够的。 (此外,php 可以自动将带有空格的字符串转换为数字。)要么检查是否有更合适的 WordPress 函数,在没有 html 的情况下返回您想要的数字,要么使用正则表达式/子字符串来提取数字。
http://plugins.svn.wordpress.org/gd-star-rating/tags/1.9.9/code/fn/legacy.php says "@return string html with rendered contents" in the docs. It is probably not just returning the number, or whitespace along with it, but html-tags (you can check this by using 'view source' on the output result. If you just view it in the browser, it will parse the html-tags and you won't see them.)
A simple trim isn't enough. (Besides, php can autoconvert the string with spaces to a number.) Either check for a more appropriate wordpress function that gives it back number you want without html, or use a regexp / substring to extract the number.
修剪空白,它应该正确比较。
Trim the whitespace and it should compare correctly.