I have a variable $id
from URL parameter that contains a name ID-number like 1234.5Z
How can I use fopen
to write a file called 1234.5Z.json
?
I tried the following syntax:
$fp = fopen($id+'.json', 'w');
But I end up with a file named 1243.5
In PHP, you don't concatenate strings using the +
sign, you use the .
instead.
So, your code will look more like
$fp = fopen($id . '.json', 'w');
What happens when you were using the +
, is that PHP was thinking you were doing an arithmetic operation, and thus, it does it's best to parse the number within the first string, and as a result 1234.5Z -> 1234.5
, and .json -> 0
, and therein 1234.5 + 0 = 1234.5
, which then will be used as the string for the name.