Fabio
Fabio

Reputation: 85

compare 2 variables in php

if ($ct >= $count){
  echo "ct=$ct and count=$count</br>";
  echo "a";     
  return 0; //No record deleted in the datatable
}elseif ($ct < $count){
  echo "ct=$ct and count=$count</br>";
  echo "b";     
  return 1; //Record deleted in the datatable
}

The output is:

ct=1 and count=2
a

That means "1 >= 2" is true.... How is that possible??? How can I fix it???? I'm driving crazy with this code ... That's the first time happening something like that

Upvotes: 0

Views: 83

Answers (1)

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41820

if $ct is a boolean, then in this expression:

if ($ct >= $count){ 

$count will be converted to a boolean for the comparison. A non-zero number will evaluate to true, so the condition $ct >= $count will be satisfied, because true == true.

In this expression:

echo "ct=$ct and count=$count</br>";

The boolean true is converted to a string, and the string equivalent of true is '1'.

Upvotes: 2

Related Questions