从数组中减去只删除前2个字符?

I'm having an issue where I'm trying to subtract 1 from a number. This number is stored in an array. The array is formed by reading a line from a text file, splitting it by "|" and then storing it into an array.

The text file looks like this: 100|2

Here's my code:

function remove1($key, $privorno=0) {
  $actualFile = file_get_contents("users/" . $key . ".txt");
  $numtoremove = explode('|', $actualFile);

  if($privorno == 1) {
    file_put_contents("users/" . $key . ".txt", $numtoremove[0] . "|" . $numtoremove[1] - 1);
  } else {
    file_put_contents("users/" . $key . ".txt", $numtoremove[0] - 1 . "|" . $numtoremove[1]);
  }

When this executes it leaves the text file as just "100" (I.E Deleting the 2 and the "|")

Any help would be great. Thanks!

I'm surprised, that you get 100 in your file. So I put a simplified reproducer on 3v4l. For you if branch it prints 99 (not 100), but it seems to remove |2.

This can be explained by the operator precedence in PHP (manual). Both . and - have the same priority, and they are left associative. This means that the string 100|2 gets created first, and then PHP tries to subtract 1, which should result in 99.

As I said, I don't know why you get 100, but maybe you can get your program to work as intended by adding parentheses in the right place:

file_put_contents("users/" . $key . ".txt", $numtoremove[0] . "|" . ($numtoremove[1] - 1));