简体   繁体   English

PHP的基于整数的IF语句不显示结果?

[英]php if-statement based on integers is not displaying results?

I'm using the following preg_match : 我正在使用以下preg_match

preg_match( '!<div class="thumblock ">(.*)</div>!si' , wp_gdsr_render_article_thumbs(0, false, "", 0, "", false) , $n );
$thumbs_string = strip_tags( $n[1] );

To extract the number between the span tags: 要提取span标签之间的数字:

<div class="thumblock ">
<span class="rating-result">
<div id="gdsr_thumb_text_12_a" class="gdt-size-20 voted inactive gdthumbtext">+1</div>
</span>
<div class="ratingtext ">
<div class="raterclear"></div>
</div>

(in the example above, result is a string: "+1") (在上面的示例中,结果为字符串:“ + 1”)

So I tried converting it into an integer with this: 因此,我尝试将其转换为整数:

$thumbs_number = (int)$thumbs_string;

which is used in this function: 在此功能中使用的:

function get_rating_class($thumbs_number) {
    if ($thumbs_number < 0) return ' bad';
    if ($thumbs_number < 2) return ' average';
    if ($thumbs_number < 4) return ' good';
    return ' excellent';
}

function rating_class($thumbs_number) {
    echo get_rating_class($thumbs_number);
}

to output a div class: 输出div类:

<div class="topic-like-count<?php rating_class($thumbs_number); ?>">

I even did var_dump() : 我什至做了var_dump()

<h2><?php var_dump($thumbs_string); ?></h2>
<h2><?php var_dump($thumbs_number); ?></h2>

and the results were: 结果是:

string(2) "+1" and int(1) respectively (I directly copy/pasted them here). string(2) "+1"int(1) (我直接在此处复制/粘贴)。

But no div class is being output. 但是没有div类输出。

Any suggestion to fix this? 有什么建议解决这个问题吗?

EDIT: 编辑:

The class is indeed being output in the HTML source, but it isn't having any effect (and my stylesheet is not being cached). 该类确实已在HTML源代码中输出,但是没有任何效果(并且我的样式表没有被缓存)。 I have another version of the function which doesn't add an extra div around the span tags, and that one works but unfortunately I need that div. 我有该函数的另一个版本,该版本没有在span标签周围添加额外的div,并且该版本可以工作,但不幸的是我需要该div。

If the class name is being displayed in the HTML then the PHP code is fine. 如果类名称显示在HTML中,则可以使用PHP代码。 The stylesheet is likely the problem. 样式表可能是问题所在。

Sure.. add an echo before the call to rating_class($thumbs_number); 当然..在调用rating_class($thumbs_number);之前添加echo rating_class($thumbs_number); :

<div class="topic-like-count<?php echo rating_class($thumbs_number); ?>">

Why are you using an extra function that's only used to echo? 为什么使用额外的仅用于回显的功能? The following should work just fine. 以下应该可以正常工作。

<div class="topic-like-count<?=get_rating_class($thumbs_number);?>">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM