Connecting Tech Pros Worldwide Help | Site Map

reading variables in a filename

  #1  
Old June 22nd, 2009, 05:43 PM
Member
 
Join Date: Jun 2009
Posts: 52
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.
  #2  
Old June 22nd, 2009, 05:48 PM
Member
 
Join Date: Jun 2009
Posts: 52

re: reading variables in a filename


oh..okay..i figured it out. I forgot to add the "." in before and after my variable.=)
  #3  
Old June 22nd, 2009, 06:13 PM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,862
Provided Answers: 9

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.  
  #4  
Old June 22nd, 2009, 06:30 PM
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,075

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
  #5  
Old June 22nd, 2009, 06:34 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,701
Provided Answers: 4

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 *
  #6  
Old June 22nd, 2009, 07:15 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,487
Provided Answers: 9

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}()
  #7  
Old June 22nd, 2009, 08:57 PM
Member
 
Join Date: Jun 2009
Posts: 52

re: reading variables in a filename


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Session variables disappear in relation to a file upload Matt Jensen answers 4 December 7th, 2005 04:45 PM
converting variables Nate answers 20 November 14th, 2005 07:26 AM
Reading and Writing to Binary Files Daniel Moree answers 7 July 22nd, 2005 11:15 PM
reading files Harry Overs answers 3 July 22nd, 2005 07:18 PM