Domanda

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

È stato utile?

Soluzione

You have an error in your if statements.

if ($counter % 10 = 2)

should be

if ($counter % 10 == 2)

Altri suggerimenti

Try the following statement. Your statement is wrong written

if ($counter % 10 == 2)

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top