-2

I've been a developer for decades - in many programming languages. I can't get a very simple straight forward "if" statement to work in PHP:

if($groupminc == $groupmaxc)

    {
     echo "AFTER1:GROUPMIN==>" . $groupminc . "\n";
     echo "AFTER1:GROUPMAX==>" .  $groupmaxc . "\n";
     $groupmsg = $groupminc . " Participant";
    }
  else
   {
     echo "AFTER2:GROUPMIN==>" . $groupminc . "\n";
     echo "AFTER2:GROUPMAX==>" .  $groupmaxc . "\n";
     $groupmsg = $groupminc . " to " . $groupmaxc . " Participants";
   }

I'm getting the AFTER1 happening even when $groupminc and $groupmaxc are clearly not equal.

  • 3
    try to debug $groupminc and $groupmaxc before if statement – Moyed Ansari Jun 15 '12 at 17:10
  • 5
    What are the values of $groupminc and $groupmaxc? – ChrisW Jun 15 '12 at 17:10
  • 2
    PHP is losely typed... if you want to compare type as well as value then use the `===` operator otherwise things will be dynamically cast to "what makes sense as far as i can tell"... – prodigitalson Jun 15 '12 at 17:12
  • Post the result you get from `echo`. Or use `var_dump($groupminc);` – Simon Forsberg Jun 15 '12 at 17:13
  • 4
    Before trying anything it is better if you clarify the types of `$groupminc` and `$groupmaxc`. Otherwise you will get a half-assed answer and you will definitely will be back later. – Alexander Jun 15 '12 at 17:14
  • 3
    For being an experienced developer, it should be practice to consult the fine manual ... e.g. [the section on `==`](http://www.php.net/manual/en/language.operators.comparison.php). Also, trying things out on a REPL (or "codepad") can help decrease the time it takes to absorb different rules/heuristics/concepts. –  Jun 15 '12 at 17:15
  • possible duplicate of [How do the equality (== double equals) and identity (=== triple equals) comparison operators differ?](http://stackoverflow.com/questions/80646/how-do-the-equality-double-equals-and-identity-triple-equals-comparis) –  Jun 15 '12 at 19:16

4 Answers4

2

try with === :

if($groupminc === $groupmaxc)
mgraph
  • 14,736
  • 4
  • 36
  • 72
  • 2
    If they are **clearly not equal** then I guess that means that they're content isn't equal, not just their type. This probably won't help at all. – Simon Forsberg Jun 15 '12 at 17:12
  • 3
    @Simon: id put money on this actually being the issue/solution *clearly not equal* is a subjective phrase... to someone used to other languages `String(0)` may be clearly not equal to `Integer(0)` – prodigitalson Jun 15 '12 at 17:15
  • @prodigitalson I am starting to think that you are right, I just realized (after getting a bug on my own) that a non-empty string (excluding some special ones) compared to the integer 1 equals true. That could very well be the case here. Because after all, the string "bug" is "clearly not equal" (according to even myself) to the integer 1. – Simon Forsberg Jul 08 '12 at 00:24
0

I think the issue you're finding here is the difference between the == and the === operators. There's a pretty good answer at this other stackoverflow question

Community
  • 1
  • 1
McAden
  • 12,757
  • 4
  • 35
  • 60
0

Show us your complete code with the initialization of the variables.

Or just try following:

echo ($groupminc !== $groupmaxc ? $groupminc.' to ' : '').$groupmaxc.' Participants';
DragonWork
  • 2,285
  • 16
  • 20
0

You have to be more clear, what values you get in $groupminc and $groupmaxc.Otherwise there may be issue of == and ===

Rakesh Shetty
  • 4,228
  • 6
  • 35
  • 76