0

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";
}
}
?>
  • You seem to be mixing C or Java or something with PHP – Mat Jan 21 '18 at 09:17
  • Yeah not really familiar with php. – user9246796 Jan 21 '18 at 09:18
  • @slevy1: that's what basic tutorials are for, not Stack Overflow. – Mat Jan 21 '18 at 11:04
  • The OP's question must be re-opened. Even the person(s) who closed it neglect to recognize an extremely important flaw in the code that no one has addressed yet but myself (see my response below), namely the use of tainted data. – slevy1 Jan 23 '18 at 20:07

2 Answers2

1

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";
}
}
?>
slevy1
  • 3,797
  • 2
  • 27
  • 33
Pankaj Kant Patel
  • 2,050
  • 21
  • 27
0

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

slevy1
  • 3,797
  • 2
  • 27
  • 33