在php中添加字符串和数字(​​FAULT !!)

I'm having little trouble with this code that reads ordernumbers from txt file, then adds 1 to that number. However PHP does not add these numbers together regardless of automatic type-casting...

    $handle1 = fopen("ordernumbers.txt", "r");
    $numberoforders = fgets($handle1);
    $numberoforders = trim($numberoforders);
    $orderid=$numberoforders+1;
    echo $orderid;

When echoing $numberoforders, it returns number 5 (type is string)

When echoing $orderid, it returns 1, while it should give 6.

I can not see any problem here, and it still does not work. I tried also to change that variable type from string to int, and then add these numbers together, but same result (1).

edit:here's the contents of the txt file: http://imgur.com/bzjOuOJ

Because $numberoforders is converted into integer and its value is 0, so 0 + 1 =1 that's the output you are getting . If you want the string's characters number add to the integer value, you need to count the string's length first.

You can get your result like so

  $handle1 = fopen("ordernumbers.txt", "r");
  $numberoforders = fgets($handle1);
  $numberoforders = trim($numberoforders);
  $numberoforders  = strlen($numberoforders);
  $orderid=$numberoforders+1;
  echo $orderid;

As your var_dump gives string(4) ''5'' to string stored in $numberoforders must contain three more characters.

I suspect a linebreak or something similar