2

I am trying to use a modulus in my if statement, however I cannot get it working or find an example of this anywhere and it is not the same as in other languages I have done. So far ive tried

if ($counter % "10" = 2)
  {
    echo ($counter);
  echo "Have a good morning!";
  }

and

if ($counter % 10 = 2)
  {
    echo ($counter);
  echo "Have a good morning!";
  }

and

if (($counter % "10") = 2)
  {
    echo ($counter);
  echo "Have a good morning!";
  }

Yet still nothing is working, I am getting the error

Parse error: syntax error, unexpected '=' in H:\STUDENT\S0190204\GGJ\index.php on line 50

Although that is probably because the syntax is incorrect, if anyone could shed some light on this or point me in the direction of a site that shows a modulus used in an if statement in php Id appreciate it. Thank you

2
  • 1
    = assignment; == comparison Commented Feb 11, 2014 at 13:02
  • Yo u should use == for checks. = is for assignment Commented Feb 11, 2014 at 13:02

3 Answers 3

5

You have an error in your if statements.

if ($counter % 10 = 2)

should be

if ($counter % 10 == 2)
Sign up to request clarification or add additional context in comments.

Comments

0

Try the following statement. Your statement is wrong written

if ($counter % 10 == 2)

Comments

-1

Yes above is right use compare operator(==) not assignment(=) and also take care of operator precedence by using () try this if (($counter % 10) == 2)

2 Comments

Operator Precedence is only needed in this case if you assign like ($result = ($counter % 10)) . Otherwise it's pretty redundant and doesn't help clarity in this case at all.
Yes i agree with you but i think, it is good practice to take care of precedence always :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.