0

Here is my php script:

<?php
  $id = "";
  $name = "";
  $original = 0;
  $new = 0;
  $difference = 0;
  $message = "";
  $fin = fopen("inv.txt", "r");
  printf("                      SALES REPORT");
  printf("\nITEM ID     ITEM NAME   SALES DIFF    MESSAGE\n");
  while(! feof($fin))
  {
    $info = explode(" ", $info);
    $id = $info[0];
    $name = $info[1];
    $difference = strval((int)$new - (int)$original);
    if ((int)$difference < 0)
    {
      $message = "You're Fired!";
    elseif ((int)$difference > 0)
    {
      $message = "Bonus!";
    else
    {
      $message = "Pick it up!";
    }
    printf($id + "          " + $name + "          " + $difference + "          " + $message + "\n");
  }
?>

Here is the content of inv.txt:

W111 widget 20 25
B222 Bob    34 30
T333 Thingy 10 10
S444 Something 45 44

The output I am trying to get from my php script is

                      SALES REPORT
ITEM ID     ITEM NAME     SALES DIFF     MESSAGE
W111          widget          -5          You're Fired!
B222          Bob              4          Bonus!
T333          Thingy           0          Pick it up!
S444          Something        1          Bonus!

But instead I get

Parse error: syntax error, unexpected token ")" in C:\Users\user\Documents\file.php on line 10

What did I do wrong and how do I fix it?

TheRealTengri
  • 1,201
  • 1
  • 7
  • 17
  • Which line on your code is line 10? – Keale Feb 02 '21 at 02:23
  • @Keale ```printf("\nITEM ID ITEM NAME SALES DIFF MESSAGE\n");``` – TheRealTengri Feb 02 '21 at 02:24
  • The code you have posted would not give that error. However it will give a syntax error for the missing `}` before the `elseif` and the `else`. – Nick Feb 02 '21 at 02:25
  • 1
    Why are you concatenating strings in `printf()`? The whole point of `printf()` is that you use a format string and arguments, and then it replaces the formatting operators with the arguments. – Barmar Feb 02 '21 at 02:25
  • 1
    `printf("%d %s %d %s\n", $id, $name, $difference, $message);` – Barmar Feb 02 '21 at 02:26
  • 1
    The `+` operator on strings is not going to do what you intend anyway. It will cast all those strings to int 0. The dot `.` operator for string concatenation if you intend to use it. – Michael Berkowski Feb 02 '21 at 02:31

0 Answers0