I need to set a dynamic file name in PHP. So I wrote a small example script to represent the problems I am facing.
When I run the following script, I get the following erroneous output, and the file created is just named .csv
while it should be named 0101.csv
OUTPUT:
Notice: Undefined variable: 65 in C:\xampp\htdocs\testsEight.php on line 5
Notice: Undefined variable: 65 in C:\xampp\htdocs\testsEight.php on line 7
Array ( [0] => BillClinton )
Why does it call the variable as 65
rather than $the_id
? I am trying to follow these guidelines. In the following code, I also tried to replace it by ${$the_id}
, no luck!
CODE:
<?php
$type = 'BillClinton';
$the_id = 0101;
file_put_contents ( 'C:/'.$$the_id.'.csv' , $type ."," , FILE_APPEND );
$file = fopen('C:/'.$$the_id.'.csv', 'r');
$line = fgetcsv($file);
array_pop($line);
if ($line !== FALSE) {
//$line is an array of the csv elements in a line. The fgetcsv() function parses a line from an open file, checking for CSV fields.The fgetcsv() function stops returning on a new line, at the specified length, or at EOF, whichever comes first.
print_r($line);//check
} else {echo 'FALSE';}
Please help me fix this.
You're using two $
in $$the_id
and one in $the_id = 0101;
"Why does it call the variable as 65"
The leading zero is treating 0101
as an octal, so wrap it in quotes $the_id = "0101";