vendredi 9 janvier 2015

PHP: Why comparing these two numbers keeps getting the wrong result? (with an if statement)

I have a Drupal site, but I think this isn't a Drupal question, but a PHP one.


I have this taxonomy field that I get printed out using tokens and Display Suite Module.


So, if I print: [node:field-suscriptor-estado1:tid] I get the term id of that particular node.


In order to show some differences between different terms, I try to compare the term ID to the one that I've got stored in database, and if they do match, it shows the result in one color. If they don't, the color is different:



<?php
$tid = "[node:field-suscriptor-estado1:tid]"; //I assign the term id to a variable
echo $tid; // I tested to see if I get it printed out using php, and it does print it: 870
$tidNum = (int)$tid; // I transform the string variable to a number

//now, if the number that the variable $tidNum has is 869 print the field with the class suscriptor-estado-abierto, else, print it with the class suscriptor-estado-cerrado.

if ($tidNum == 869) {
echo "<div class='suscriptor-estado-abierto'>[node:field_suscriptor_estado1]</div>";
} else {
echo "<div class='suscriptor-estado-cerrado'>[node:field_suscriptor_estado1]</div>";
}
?>


My question is, why if the number that I do get is 870, it keeps printing it out as if the did match? (with suscriptor-estado-abierto class). The node in question can only have one of the two options (869 or 870).


I've tried: ($tidNum == 869) and ($tidNum === 869) just in case. The result is the same. I've tried also with a new if, asking for $tidNum == 870. The result is the same.


Please, if you downvote my question, leave a comment about the reason why you did it. That way I'll ask better or in a more accurate way next time. That's the only way of improving. Thanks! Cheers from Buenos Aires.


Aucun commentaire:

Enregistrer un commentaire