The problem is with the cast dumb and nosy PHP.
According to the manual (free translation):
If you compare a number with a string or the comparison consists of numerical strings, then each string will be converted to a number and the comparison will occur numerically. [... ] This conversion does not apply when the comparison is made with the operators === or !== since this characterizes comparison by value and by type.
That said, on your test:
if( $teste == 0 )
The left side is first being interpreted as the variable it represents.
The right side, because it is a number "suggests" to the interpreter that the comparison should be numerical and with this the string that the variable represents, passes, internally, to be a number as well.
It’s like it’s being done:
if( (int) $teste == 0 )
And since a string cannot be a (obvious) number, the result of the conversion becomes zero and the rest is logical.
To solve this kind of subtle and difficult debugging problem use typed comparison as the manual says:
if( $teste === 0 )
And the link will be created.
Apart from that, I leave as a suggestion that when you are generating HTML with PHP prefer to use alternative syntax control structures as it becomes MUCH easier to understand the open-and-close of tags:
<?php if( $produto['fold'] === 0 ) : ?>
---
<?php else : ?>
<a href="<?= $produto['fold']; ?>">Link</a>
<?php endif; ?>
Any particular mistake, or something I can help you figure out? $product['fold'] has what value?
– Renato Tavares
It has either a link or 0,however the code works,so it enters both if and Else,and I don’t understand why
– Rodolfo Oliveira
Are you sure @Rodolfooliveira, the problem is definitely not the code being passed on
if
and in theelse
of a samestatement
, why as soon as he enters theif
, has no reason to go toelse
and gets into theelse
was because it did not satisfy the imposed condition... There is a looping out of thatif
?– Daniel Ribeiro
I just copied and run this snippet of code here and is not with the problem that you reported, even because it is impossible to enter the
if
and in theelse
at the same time. Show us what you have around this code.– Lucas
For starters, are you sure PHP is interpreting your code? How are you testing?
– Bacco
Edited. Now the topic makes sense and can be reopened.
– Bruno Augusto
@Rodolfooliveira try to understand what the problem really is. Add more information from your code to the question.
– Daniel Ribeiro
I did not remove the last sentence just to not violate the intention of the OP, but it is a logical test. Yes, yes, no, soon you enter the
If
, cannot be entering theelse
also.– Bruno Augusto
What do you mean, @Brunoaugusto? What he says is happening is impossible! I think Bacco is intuiting the real problem, the code is not being interpreted and so much the
---
when the link is being printed. Maybe it’s simply the case that you don’t use shorttags. Or actually access the server if the access is being done by file:///. Only that the author needs to clarify what he is doing before we can reopen. Because for me it is still not clear.– bfavaretto
Well, I understood the question, isolated the problem and drafted an explanatory answer. I’m just waiting for the topic to reopen only.
– Bruno Augusto
@Okay, I’m reopening, post your answer.
– bfavaretto
@Rodolfooliveira this problem arises even when using PHP tags complete? that is to say
<?php
to open and?>
to close?– Sergio
Gee... I almost missed my chance because I didn’t see the topic being reopened. Thanks for the vote of confidence.
– Bruno Augusto