0

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.

Solace
  • 8,612
  • 22
  • 95
  • 183
  • 1
    You're using two `$` in `$$the_id` and one in `$the_id = 0101;` however that leading zero could be a factor also (being read as an octal?) so you may want to wrap it in quotes `$the_id = "0101";` – Funk Forty Niner Aug 23 '14 at 20:48
  • @Fred-ii- Firstly, thank you very much. Secondly, my problem is that in the real program the `$the_id` is coming from a database. So I am not very sure if it is a string (in quotes) or a number. So is there a way to ensure that if it is a number, it remains intact and not get converted? – Solace Aug 23 '14 at 20:57
  • You're very much welcome Zarah, *cheers* – Funk Forty Niner Aug 23 '14 at 20:59

3 Answers3

2

You had extra $ in $$the_id which lead to call the reference of $the_id intead of variable name the_id. So you need to erase that. Code will be as follow;

<?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';}

for more details, you can look in PHP documentation

Naing Lin Aung
  • 3,373
  • 4
  • 31
  • 48
  • [`I said that`](http://stackoverflow.com/questions/25466175/php-create-dynamic-variable-file-name-undefined-variable-65#comment39740447_25466175) 4-5 minutes prior. OP wants to use variables variable http://php.net/manual/en/language.variables.variable.php – Funk Forty Niner Aug 23 '14 at 20:53
  • sorry, I didn't see that. – Naing Lin Aung Aug 23 '14 at 20:54
  • 1
    Plus, OP states *"Why does it call the variable as 65"* - that's because of the unquoted `0101` is being treated as an octal; which I also stated. – Funk Forty Niner Aug 23 '14 at 20:55
  • 1
    I'm glad that what I want can be done without the hasel of variable variables. But the problem is I do not want the unquoted `0101` to be treated as octal, because it is coming from a database, so I do not have control to put `"` around it. – Solace Aug 23 '14 at 21:01
  • @Zarah, it doesn't matter if it's a `string` or an `integer`. When concatenated with the `'C:/'.$id.'.csv'` the `$id` becomes a string regardless of it's initial type. As a tip, you CAN control it's type if you want to, it's called `type-casting`, (string)$the_id will return `0101` as string `'0101'`. Same with `(int)`, `(bool)`, `(float)` etc. – Eduard Aug 23 '14 at 21:10
2

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";

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Firstly, thank you very much. Secondly, my problem is that in the real program the $the_id is coming from a database. So I am not very sure if it is a string (in quotes) or a number. So is there a way to ensure that if it is a number, it remains intact and not get converted? – Solace Aug 23 '14 at 20:59
  • @Zarah You're welcome. Let me think about that one. However, if you keep it as `$the_id = "0101";` etc., can you keep using that? – Funk Forty Niner Aug 23 '14 at 21:01
  • No, I hard-coded `0101` for this small example. In the real program, `$the_id` comes from a database. And I am not sure if it is a string (quoted) or a number (unquoted). – Solace Aug 23 '14 at 21:03
  • @Zarah How is it coming from DB, can you show a quick example? You may have to ask another question if either of us can't answer it. – Funk Forty Niner Aug 23 '14 at 21:03
  • I am working on a Moodle plugin, and using Moodle's API to query the database. The query returns numeric value like `24` etc. That's what I found out by printing it. But I don't think I can connect to Moodle's database from this example. I don't have much information about the database either. – Solace Aug 23 '14 at 21:08
  • 1
    @Zarah I don't have knowledge of that plugin, but I'll see what I can find out about it. If I can't or anyone else that gave an answer, you may have to ask another question, showing how you're pulling in from it, and tagging as `Moodle` also, and `mysql` - `mysqli` if that's the API you are using. – Funk Forty Niner Aug 23 '14 at 21:10
  • 1
    @Zarah You can also try `$the_id = int($variable);` – Funk Forty Niner Aug 23 '14 at 21:13
  • Thank you very much. I am just going to assume that it is a string. Then if it does not work fine, I'll post another question. – Solace Aug 23 '14 at 21:14
  • I just assumed that it is quoted and is a string, and proceeded that way. It works perfectly fine. So let's think it was a string. Thank you very much. – Solace Aug 23 '14 at 21:27
1

Firstly, your example is wrong. PHP will never allow you to define integer and not even "string-casted" integers as a variable name.

The only problem in your script is that you are using double dollar signs, which is a refference to $0101 (assuming the $the_id is 0101 string or integer, doesn't matter).

The simple solution is to remove your double dollar signs in:

file_put_contents ( 'C:/'.$the_id.'.csv' , $type ."," , FILE_APPEND );

$file = fopen('C:/'.$the_id.'.csv', 'r');

The idea behind this is that a variable's name can be variable. And that's how your problem rose.

$a = 'hello';
$$a = 'world';

echo $hello; // will output 'world';

Regards.

Eduard
  • 3,395
  • 8
  • 37
  • 62
  • After removing the extra `$`, it does write the file and then read it as well. The only problem as mentioned by @Fred-ii- is that when `0101` is an integer (that is it is not quoted), it is treated as octal, so converted to `65` and the file is named as `65.csv` – Solace Aug 23 '14 at 21:12
  • Upvoted because the `$$` was actually the problem. But as I mentioned in the comment above, I am not clear about the part of the answer which argues that "assuming the $the_id is 0101 string or integer, doesn't matter".. I'll appreciate if you can clarify this, so that I can learn what you tried to explain. Thank you. – Solace Aug 23 '14 at 21:30
  • 1
    @Zarah sorry for the late reply. When you manually define a variable like `$var` in PHP, you will NOT be able to define it's name using integers-only. Meaning, `$0101` is impossible and will result in a fatal error, and keeping that in mind, you think about that fact that $the_id is being returned as `integer` from the database (or so I guess, if it's an index it's most likely to be integer). Either way, when joining an integer type var into a string, it will be concatenated and the result will be `string`. Even if `$the_id` is int or string, the result will still be string. Hope you get it. – Eduard Aug 25 '14 at 20:14
  • Yes I get it. I had this question in my mind that if we concatenate an integer with a string, does this result in a string. So Thank you very much. – Solace Aug 26 '14 at 06:23