【发布时间】:2014-10-17 10:31:40
【问题描述】:
我需要在 PHP 中设置一个动态文件名。所以我写了一个小示例脚本来代表我面临的问题。
当我运行以下脚本时,我得到以下错误输出,并且创建的文件刚刚命名为 .csv 而它应该命名为 0101.csv
输出:
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 )
为什么将变量称为65 而不是$the_id?我正在尝试关注these guidelines。在下面的代码中,我也尝试将其替换为${$the_id},没有运气!
代码:
<?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';}
请帮我解决这个问题。
【问题讨论】:
-
您在
$$the_id中使用了两个$,在$the_id = 0101;中使用了一个,但是前导零也可能是一个因素(被读取为八进制?)所以你可能想要包装它引号$the_id = "0101"; -
@Fred-ii- 首先,非常感谢。其次,我的问题是,在实际程序中,
$the_id来自数据库。所以我不太确定它是一个字符串(引号)还是一个数字。那么有没有办法确保如果它是一个数字,它保持不变并且不会被转换? -
不客气,Zarah,干杯
标签: php file csv filenames variable-variables