Connecting Tech Pros Worldwide Forums | Help | Site Map

reading variables in a filename

Member
 
Join Date: Jun 2009
Posts: 52
#1: Jun 22 '09
I believe this is a simple problem but I'm not able to figure it out. I have a file that has a filename on each line. I read through this file setting the string to a variable. Then I am trying to use this variable in opening the file but it won't read it. This is my current code.
Expand|Select|Wrap|Line Numbers
  1. while (! feof($ts_proj)){
  2.         $filename= fgets($ts_proj);
  3.                 $codes=fopen('/projects/$filename/ts_codes',"r");
  4. }
  5.  
The error says it cant open /project/$filename/ts_codes instead of /project/project1/tscodes which means the variable is passing through. Any help is appreciated.

Member
 
Join Date: Jun 2009
Posts: 52
#2: Jun 22 '09

re: reading variables in a filename


oh..okay..i figured it out. I forgot to add the "." in before and after my variable.=)
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#3: Jun 22 '09

re: reading variables in a filename


Quote:

Originally Posted by phpnewbie26 View Post

oh..okay..i figured it out. I forgot to add the "." in before and after my variable.=)

Single quotations will not parse the string for variables. Double quotes, however, will.

Expand|Select|Wrap|Line Numbers
  1. $name = "Mark";
  2. echo "My name is $name.";
  3. // Output: My name is Mark.
  4.  
For readability's sake, if you use this technique, wrap the variable in curly braces.
Expand|Select|Wrap|Line Numbers
  1. $name = "Mark";
  2. echo "My name is {$name}.";
  3.  
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#4: Jun 22 '09

re: reading variables in a filename


besides better readability I think the curly braces were used in cases where your variable is not surrounded by space.

e.g. : this won't work:
echo "6584564$someNum2345";

solution:
echo "6584564${someNum}2345";

:)

Cheers,


Dan
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#5: Jun 22 '09

re: reading variables in a filename


Quote:

Originally Posted by dlite922 View Post

echo "6584564${someNum}2345";

Interesting. I didn't know putting the $ outside the braces worked to.

I always do
Expand|Select|Wrap|Line Numbers
  1. echo "6584564{$someNum}2345";
But they both seem to work :D

I wonder if there are any differences...
* Goes for the Google bookmark *
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#6: Jun 22 '09

re: reading variables in a filename


Quote:

Originally Posted by Atli View Post

I wonder if there are any differences...

there are some edge cases, mostly concerning dynamic variables and dynamic function calls.
Expand|Select|Wrap|Line Numbers
  1. $$array[1] vs. ${$array[1]}
  2. $obj->{$method}()
Member
 
Join Date: Jun 2009
Posts: 52
#7: Jun 22 '09

re: reading variables in a filename


thanks guy! This is all very helpful!=)
Reply