Search the database and compare

Asked

Viewed 284 times

-3

I have a simple code whose goal is to get the value of a field and make a comparison, if the value is zero, do one thing, otherwise do another.

However I must be missing something, because it is not working as expected.

That was my attempt:

<?

$teste=$produto['fold'];

if($teste==0) {

?>

---

<?

} else {

?>

<a href="<?= $produto['fold']; ?>">Link</a>

<?

}

?>

With this code he enters both if how much in the else. Where is my mistake?

  • Any particular mistake, or something I can help you figure out? $product['fold'] has what value?

  • It has either a link or 0,however the code works,so it enters both if and Else,and I don’t understand why

  • 2

    Are you sure @Rodolfooliveira, the problem is definitely not the code being passed on if and in the else of a same statement, why as soon as he enters the if, has no reason to go to else and gets into the else was because it did not satisfy the imposed condition... There is a looping out of that if?

  • 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 the else at the same time. Show us what you have around this code.

  • 3

    For starters, are you sure PHP is interpreting your code? How are you testing?

  • Edited. Now the topic makes sense and can be reopened.

  • @Rodolfooliveira try to understand what the problem really is. Add more information from your code to the question.

  • 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 the else also.

  • 2

    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.

  • Well, I understood the question, isolated the problem and drafted an explanatory answer. I’m just waiting for the topic to reopen only.

  • @Okay, I’m reopening, post your answer.

  • @Rodolfooliveira this problem arises even when using PHP tags complete? that is to say <?php to open and ?> to close?

  • Gee... I almost missed my chance because I didn’t see the topic being reopened. Thanks for the vote of confidence.

Show 8 more comments

1 answer

2

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; ?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.