在php中发现解析错误[重复]

This question already has an answer here:

I am new to php. So I simply translated my java code to php.

It says

Parse error: syntax error, unexpected 'counter' (T_STRING) in D:\files\xampp\htdocs\try.php on line 15.

Here is my code:

<form action="" method = "POST">

Enter the first number: <input type="number" name="FirstNum" required><br><br>

<input type = "submit" value = "Enter"/>
</form>


<?php
int counter = 0;
while (counter < 1) {

switch($_POST['FirstNum'])
{
case "1":
echo "Addition";
counter++; break;
case "2":
echo "Subtraction";
counter++; break;
case "3":
echo "Multiplication";
counter++; break;
case "4":
echo "Division";
counter++; break;
default:
echo "Invalid Operation";
}
}
?>
</div>

PHP variables always start with a $ sign, so whenever you use a variable the $ sign is mandatory.

The corrected code looks like:

<?php
$counter = 0;
while ($counter < 1) {

switch($_POST['FirstNum'])
{
case "1":
echo "Addition";
$counter++; break;
case "2":
echo "Subtraction";
$counter++; break;
case "3":
echo "Multiplication";
$counter++; break;
case "4":
echo "Division";
$counter++; break;
default:
echo "Invalid Operation";
}
}
?>

The error message that the OP cites relates to the very first statement:

int counter = 0;

in which the code attempts to declare a variable's type. However, PHP users don't need to do this nor should they because PHP determines a variable's type based on its value. However, if you eliminate "int", as follows:

 counter = 0;

the code still errs out because in PHP, a variable's name consists of a bareword and a sigil in the form of a prepended dollar sign. The proper correction for the first statement is as follows:

 $counter = 0; 

Important: Always consider the data a user provides as suspect. Instead of directly using $_POST['FirstNum'], one should check that it contains valid data. Consider the following solution:

<?php
if (isset($_POST['submit'])) {
  $cleanFirstNum =  (ctype_digit($_POST['FirstNum']) )? $_POST['FirstNum'] : "0";
  $counter = 0;
  while ($counter++ < 1) {
    switch( $cleanFirstNum )
    {
      case "1":
        echo "Addition";
        break;
      case "2":
        echo "Subtraction";
        break;
      case "3":
        echo "Multiplication";
        break;
      case "4":
        echo "Division";
        break;
      default:
        echo "Invalid Operation";
        break;
    }   
  }
}
?>

If $cleanFirstNum is not numeric, then it acquires a value of zero and the default code runs.

Note that with the input element of type number you may specify minimum and maximum values to restrict the user's options. The backend code in this example assures that if anyone tampers with the form, you may avoid invalid data having a deleterious effect.

<html>
<head><title>testing</title></head>
<body>
<form action="" method = "POST">
Enter the first number: <input type="number" min="1" max="4" name="FirstNum" required><br><br>

<input type="submit" name="submit" value="Enter"/>
</form>
</body>
</html>


Finally, one last concern: should the OP's code use PHP or might JavaScript be more appropriate (see a related example here)?

Popular PHP resource: the Manual