Why FormatException: Input string was not in the correct format?

String looks like 8. But I cant convert it to number. Code:

public void UpdCoins(string str)
	{
		Write ("strCoins="+str+" uid="+UserId+" next ch ");
		Write ("="+str+"=");// returns =8=
		Write (("8"==str[0].ToString()).ToString());// returns False

		// declare a string variable with a value that represents a valid integer
		string sillyMeme = str;

		int memeValue;
		// attempt to parse the value using the TryParse functionality of the integer type
		int.TryParse(sillyMeme, out memeValue);

		Write(memeValue.ToString());//9001


		SocialData[UserId].Coins = memeValue;
		Write ("RESULT Coins="+SocialData[UserId].Coins.ToString()+" uid="+UserId);

	}
public static void Write(string mes)
	{
		Application.ExternalCall ("Alert", mes);
		//LogClass.Mess += mes + " ";
		Debug.Log ("SM->" + mes);
	}

This pieces are in SocialManager.cs

Now piece from index.html of game

function GetCoins() {
			
			alert(viewer_id + "do");
				
				
					
				SendToPlayer("UpdCoins", '<?php include 'output.php'; ?>');
			}

function SendToPlayer(func, what){
			
				gameInstance.SendMessage("SocialManager",func,what);
				
				}

And now the full php

<?php
    include 'db.php';
    $stmt = $pdo->prepare('SELECT * FROM payments WHERE users="372380032"');
    $stmt->execute();
    $data = $stmt->fetchAll();
    $result = $data[0]['coins'];
    $pdo = null;
    echo $result.'';

?>

Why “8” cant convert to 8? Returns format error

Are you 100% sure that your output.php doesn’t have a newline character at the end after the closing ?>?

You may want to check the string like this:

Write (str+"("+str.Length+")");

Another reason could be that your output.php file might be saved with bom (byte order mask) at the beginning. It is stripped / ignored by most file processing applications but might be preserved in your string. It’s not a printable character. It’s usually better to create a class or at least a function in PHP which returns your desired data. That way you can include your php file at the top of your index html and just call the function in the place where you need the data. This avoids any included characters from the file.

Though if you want to keep it the way you have it, you can try converting each character from “str” into a hex code and print that in order to figure out what additional characters are inside your string. This method might help:

public static string ToHex(string aText)
{
    var sb = new System.Text.StringBuilder();
    foreach(char c in aText)
    {
        sb.AppendFormat("{0:X4} ", (int)c);
    }
    return sb.ToString();
}

This will turn every char of the string into a 4 digit hex number.