reading variables in a filename 
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. -
while (! feof($ts_proj)){
-
$filename= fgets($ts_proj);
-
$codes=fopen('/projects/$filename/ts_codes',"r");
-
}
-
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.
| 
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.=)
| 
June 22nd, 2009, 06:13 PM
|  | 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 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. -
$name = "Mark";
-
echo "My name is $name.";
-
// Output: My name is Mark.
-
For readability's sake, if you use this technique, wrap the variable in curly braces. -
$name = "Mark";
-
echo "My name is {$name}.";
-
| 
June 22nd, 2009, 06:30 PM
|  | 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
| 
June 22nd, 2009, 06:34 PM
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,701
Provided Answers: 4 | | | re: reading variables in a filename Quote:
Originally Posted by dlite922 echo "6584564${someNum}2345"; | Interesting. I didn't know putting the $ outside the braces worked to.
I always do - echo "6584564{$someNum}2345";
But they both seem to work :D
I wonder if there are any differences...
* Goes for the Google bookmark *
| 
June 22nd, 2009, 07:15 PM
|  | 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 I wonder if there are any differences... | there are some edge cases, mostly concerning dynamic variables and dynamic function calls. - $$array[1] vs. ${$array[1]}
-
$obj->{$method}()
| 
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!=)
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|