Home php PHP error “unexpected T_VARIABLE”

PHP error “unexpected T_VARIABLE”

Author

Date

Category

Error:

Parse error: syntax error, unexpected T_VARIABLE in
D: \ Work \ OpenServer \ OpenServer \ domains \ laba \ game.php on line 7

What’s wrong with this code?

& lt;? php
session_start ();
if (isset ($ _ POST ['num'])) {
  $ number = (int) $ _POST ['num'];
if (! isset ($ _ SESSION ['answer'])
  $ _SESSION ['answer'] = mt_rand (1, 10);
if (! isset ($ _ SESSION ['attempts']))
  $ _SESSION ['attempts'] = 1;
if ($ number & gt; $ _SESSION ['answer']) {
  echo 'Too big.';
  $ _SESSION ['attempts'] ++;
}
elseif ($ number & lt; $ _SESSION ['answer']) {
  echo 'Too small.';
  $ _SESSION ['attempts'] ++;
}
else {
  echo 'Hooray! You guessed right after & lt; b & gt; ', $ _SESSION [' attempts '],' & lt; / b & gt; attempts! ';
  session_destroy ();
}
}
? & gt;

Something tells me that there are several problem areas


Answer 1, authority 100%

Well, I’ll write him the answer how to write INTO not be mistaken in the elementary:

  1. Code formatting when formatting, 4 spaces are used in the context of the operator, for example, your code will look like this
  2. if and else statements must contain {}
  3. control constructs

result:

& lt;? php
session_start ();
if (isset ($ _ POST ['num'])) {
  $ number = (int) $ _POST ['num'];
  if (! isset ($ _ SESSION ['answer']) {
    $ _SESSION ['answer'] = mt_rand (1, 10);
  }
  if (! isset ($ _ SESSION ['attempts'])) {
    $ _SESSION ['attempts'] = 1;
  }
  if ($ number & gt; $ _SESSION ['answer']) {
    echo 'Too big.';
    $ _SESSION ['attempts'] ++;
  } elseif ($ number & lt; $ _SESSION ['answer']) {
echo 'Too small.';
    $ _SESSION ['attempts'] ++;
  } else {
    echo 'Yeah! You answered after & lt; b & gt; ', $ _SESSION [' attempts '],' & lt; / b & gt; attempts! ';
    session_destroy ();
  }
}
? & gt;

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions